Local Inner Classes Example in Java

If an inner class has been defined within a code block (typically within the body of a method), then such an inner class is called a local inner class. A local inner class is not a member of the enclosing class and hence it can not have any access specifier. A local inner class will have access to all the members of the enclosing class and it'll have access to the local final variables in the scope it's defined.

Program of local inner class:
class LocalInner{
   private String message = "dineshonjava.com";//instance variable
   void display(){
      class Local{
        void msg(){
          System.out.println(message);
        }
      }
     Local loc = new Local();
     loc.msg();
   }
   public static void main(String args[]){
      LocalInner obj = new LocalInner();
      obj.display();
   }
}
output:
After compiling we will get the following class files
  1. LocalInner$1Local.class
  2. LocalInner.class
Internal code generated by the compiler for local inner class:
In such case, compiler creates a class named LocalInner$1Local.class that have the reference of the outer class.
import java.io.PrintStream;
class LocalInner$1Local
{
    final LocalInner this$0;

    LocalInner$1Local()
    {   
        super();
        this$0 = LocalInner.this;
    }
    void msg()
    {
        System.out.println(LocalInner.access$000(LocalInner.this));
    }
}

Rule: Local variable can't be private, public or protected.

Rules for Local Inner class
1) Local inner class cannot be invoked from outside the method.
2) Local inner class cannot access non-final local variable.
Program of accessing non-final local variable in local inner class

class LocalInner{
   private String message = "dineshonjava.com";//instance variable
   void display(){
     int data = 20;//local variable must be final
      class Local{
        void msg(){
          System.out.println(message+" : "+data);//compile time error
        }
      }
     Local loc = new Local();
     loc.msg();
   }
   public static void main(String args[]){
      LocalInner obj = new LocalInner();
      obj.display();
   }
}

Output: Compile Time Error


Program of accessing final local variable in local inner class:
class LocalInner{
   private String message = "dineshonjava.com";//instance variable
   void display(){
     final int data = 20;//local variable must be final
      class Local{
        void msg(){
          System.out.println(message+" : "+data);
        }
      }
     Local loc = new Local();
     loc.msg();
   }
   public static void main(String args[]){
      LocalInner obj = new LocalInner();
      obj.display();
   }
}
Output:

A local inner class is defined within a method, and the usual scope rules apply to it. It is only accessible within that method, therefore access restrictions (public, protected, package) do not apply. However, because objects (and their methods) created from this class may persist after the method returns, a local inner class may not refer to parameters or non-final local variables of the method.



<<Previous <<   || Index ||   >>Next >>


No comments:

Post a Comment