Monday, 22 October 2012

Inner Classes - Java

In java inner classes are interesting. Here I am trying to present few things about inner classes. There are different types of inner classes are present in java,
     1. Regular inner classes
     2. Static inner classes (Nested classes)
     3. Method-local inner classes
Let's see these types one by one,

Regular inner classes : 
Regular inner classes means non-static inner classes, which are declared in top level class only. Here is example of regular inner class,

Code :
class TestOuter {
     private int x = 10;
     public static void main(String args[])  {
           TestOuter to = new TestOuter();
           TestOuter.TestInner ti = to.new TestInner();
           ti.testInnerMethod();   
     }

     public void outerNonStatic() {
           TestInner ti = new TestInner();
           ti.testInnerMethod();
     }

     class TestInner {
           public oid testInnerMethod() {
               System.out.print("X : " + x); //Totally valid
          }
          //Static members are not allowed in regular inner class.
         // Interfaces are no allowed here.
     }
}

From this example we can define following rules,
     1. Regular inner class can access private members of outer class.
     2. Regular inner classes can not declare any static member like static variable or method.
     3. We can directly create instance of inner class from non static method of outer class.
     4. We can create instance of inner class from static method of outer class via instance of outer class, because static method can never use this reference.
     5. We can not declare any interface in regular inner class.

Static inner classes :
Static inner classes are nested classes. Static word says that inner class is static member of outer class and can be accessed as other static members without creating instance. Here is example of static inner classes,

Code:
class Main
{
    int x =10;
    public static void main(String args[])
    {
        Test t = new Test(); // It works fine.
        t.testMethod();
        // This also works fine.
        Main.Test t = new Main.Test();
        t.testMethod();
    }
   
    public void outerNonStatic() {
        Test t = new Test();
        t.testMethod();
    }
   
    static class Test {
        public void testMethod() {
            System.out.print("X : " + x); //It will not compile.
        }
        interface TestingInt {
           
        }
    }
}

From this we can define following rules,
     1. Nested class can not access non static members of outer class.
     2. We can create instance of static class without reference of outer class. Even in static method.
     3. In static inner class we can declare any interface as well. This proves that static inner class works like top level class.

Method local inner classes:
Method local inner classes are those are defined in method. Here is simple example of method local inner classes,

Code :
class Main
{
    private int x =10;   
    public void testing() {
        Test t1 = new Test(); //It will not compile.       
       
        int a = 20;
        final int b = 30;
        class Test {
            public void testing() {
                System.out.print("X : " + x);
                System.out.print("A : " + a);// It will not compile
                System.out.print("B : " + b);               
            }
        }
       
        Test t = new Test();
        t.testing();
    }
}

From this we can define following rules,
     1. Method local inner classes can't access any variable of method except final.
     2. Object can be created only after class declaration.
     3. Can access private variables of top level class.

There are still many things to study about inner classes.

No comments:

Post a Comment