1.

Local Inner Class in Java

Answer»

A class created in a block( method body or a loop or conditional clauses) is called as a LOCAL inner class. They are not a member of the class they are enclosed in. They rather, associate themselves to the block they are defined in. Local inner class can be declared as final or abstract. They cannot be instantiated out of their SCOPE i.e.  the block they are created in.

We can say that they are a form of non-static nested classes.

For example,

PUBLIC class Example {   final int y=12;   void disp()  {     class Inner     {       void print()        {          System.out.println(y);        }     }     Inner i=new Inner();     i.print();   }  public static void main(String args[]) {   Example obj=new Example();   obj.disp();  } }

The output is as follows

$javac Example.JAVA $java Example 12


Discussion

No Comment Found