Java Local inner class

Java Local inner class

A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.

Java local inner class example

  1. public class localInner1{  
  2.  private int data=30;//instance variable  
  3.  void display(){  
  4.   class Local{  
  5.    void msg(){System.out.println(data);}  
  6.   }  
  7.   Local l=new Local();  
  8.   l.msg();  
  9.  }  
  10.  public static void main(String args[]){  
  11.   localInner1 obj=new localInner1();  
  12.   obj.display();  
  13.  }  
  14. }  

Output:
30
Previous
Next Post »