by Dinesh Thakur Category: Inheritance

A regular inner class is a nested class that only exists within an instance of enclosing class. In other words, you cannot instantiate an object of an inner class without first having an object of the outer class. An inner class can use all the methods and fields of the outer class even the private ones directly. The methods and fields of outer class are used as if they were part of the inner class.

The following program demonstrates the concept of inner class

class outer

{
      private double i = 11.5 ;
      private static String str = "Hi Dinesh";
      class inner
      {
            int j ;
            public void display()
         {
            j = 5;
            System.out.println("j = " + j);
            System.out.println("j = " + i);
            System.out.println("str = " + str);
         }
     }
}
   class InnerClass
{
     public static void main(String[] args)
     {
        outer outobj = new outer(); //create instance of enclsing class
        outer.inner innobj = outobj.new inner(); // create intansce of
                                                 //regular inner class
        innobj.display();
     }
}

Regular Inner Class Example in Java



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles