Types of Inner Class / Nested Class in Java

 There are four type of inner classes in java. There briefly,
  1. Static inner class
  2. Non static inner class
  3. Local inner class
  4. Anonymous inner class
The class which is declared with inside class is called nested class / inner class. This is classified to two types. There are, Static Nested Class & Non Static Nested Class / Inner Class.
1. Static Nested Class:
  • Nested class which is declared static is static nested class.
  • Its methods & members associates with outer class.
  • It can be used through object reference.
  •  Example:
    OuterClass.StaticNestedClass innerStatic = new OuterClass.StaticNestedClass();  
               innerStatic.display(); 
Example for Static Nested Class
 class OuterClass{  
      // Declare static nested inner classs  
      static class StaticNestedClass{  
           //static inner class method  
           void display(){  
                System.out.println("Inside Static Nested Class");  
           }  
      }  
      //outer class method  
      void display(){  
           System.out.println("Outer Class");  
      }  
 }  
 class MainClass  
 {  
      public static void main(String[] arg)  
      {  
           //how to access static nested class method  
           //1st create object for outer class object  
           OuterClass outer = new OuterClass();  
           outer.display();  
           //Now calling static nested class   
           OuterClass.StaticNestedClass innerStatic = new OuterClass.StaticNestedClass();  
           innerStatic.display();  
      }  
 }  

2. Non Static Nested Class / Inner Class:
  • It has access to the other members of the outer class, even they are private.
  • As the static declaration are not allowed inside inner class, we can't run inner class directly console.
  • We can access inner class code through outer class object only.
  • Instance of inner class valid within the enclosed class & has direct access to the methods & members of the enclosed instance.
  • For example,  
    OuterClassNonStat.NonStaticNestedClass inner = outer.new NonStaticNestedClass();
 Example for Inner Class:
 class OuterClassNonStat{  
      // Declare static nested inner classs  
      class NonStaticNestedClass{  
           //non static inner class method  
           void display(){  
                System.out.println("Inside Non Static Nested Class");  
           }  
      }  
      //outer class method  
      void display(){  
           System.out.println("Outer Class");  
      }  
 }  
 class MainClassNonStatic  
 {  
      public static void main(String[] arg)  
      {  
           //how to access non static nested class method  
           //1st create object for outer class object  
           OuterClassNonStat outer = new OuterClassNonStat();  
           outer.display();  
           //Now calling non static nested class   
           OuterClassNonStat.NonStaticNestedClass inner = outer.new NonStaticNestedClass();  
           inner.display();  
      }  
 }  

 3. Local Inner Class:
  • Local inner class is class declared in the body of method. Such class is know within contains method only. It is called Local Inner Class.
  • If we are declaring local inner class inside method, we can access both instance and static variables of outer class directly from the inner class.
  • From local inner class we are not allowed to access the local variables of the method unless it is marked as final.
  • Its not a member of enclosing class, hence doesn't have a access specifiers.
  • These inner class are very rarely used classes in real time. Because, these inner classes can be accessed with in the method only. Outside the method, your not allowed to access, violation leads to CTE.
Example for local inner class:
 class OuterClass {  
   static int x=10;  
   void m1()  
   {  
           int y=20;  
           class LocalInnerClass  
           {  
                void show()  
                {  
                     System.out.println(x); // 10  
                     System.out.println(y); // Compile time error ( If y is final no error)  
                }  
           }  
           LocalInnerClass inner=new LocalInnerClass();  
           inner.show();  
   }  
 }  
 public class MainLocalInnerClass {  
      public static void main(String args[]) {  
           OuterClass outer = new OuterClass();  
     outer.m1();  
      }  
 }  

4. Anonymous Inner Class:
  • We can declare anonymous inner class either in static or non static methods.
  • If we declare anonymous inner class in the static method the static variables of outer class can be accessed directly from inner class.
  • If we are declaring anonymous inner class inside instance method, we can access both instance and static variables of outer class directly from the inner class.
  • From anonymous inner class we are not allowed to access the local variables of the method unless it is marked as final.
  • It is accessible only at the point of its declaration.
 Example for anonymous inner class:
 interface Inter {  
      void display();  
 }  
 class AnonymousInnerClass  
 {  
      public static void main(String arg[]){  
           //Create thread using anonymous class  
           int x = 10;  
           Runnable r=new Runnable(){  
                public void run(){  
                     System.out.println("run"+x);//compile error.  
                               //local variable x accessed from inner class (if x is final then no error)  
                }  
           };  
           Thread t=new Thread(r);  
           t.start();  
           //implement interface  
           Inter inter = new Inter(){  
                public void display(){  
                     System.out.println("Display method");  
                }  
           };  
           inter.display();  
      }  
 }  

No comments:

Post a Comment