WHY Local Inner class cannot access the non-final local variables but can access final local variables.




Contents of page >
  • What are local variables in java?
  • Where do local variables live in java?
  • Where do object of local inner class live in java?
  • What java docs says about “Local Inner class cannot access the non-final local variables but can access final local variables.”
  • JVM create create a synthetic field inside the inner class in java -
  • You must be wondering, What are synthetic fields in java?
  • Program to show Local Inner class cannot access the non-final local variables in java >
  • Program to show Local Inner class can access final local variables in java >
  • Summary >


You must read about  local inner class in java  before going into this topic.


Java doesn't support closures, i.e. local variable can’t be accessed outside the method, but fields of class can be accessed from outside the class.

What are local variables in java?
All variables of the method are called local variables in java.

Where do local variables live in java?
Methods are pushed on stack so local variables live on the stack.
Local variables of the method are kept on the stack and are lost as soon as the method ends in java.

Where do object of local inner class live in java?
As object of local inner class live on the heap, objects may be alive even after method ends in which local inner class have been defined.
As, local variables of the method are kept on the stack and are lost as soon as the method ends, but even after the method ends, the local inner class object may still be alive on the heap.

What java docs says about “Local Inner class cannot access the non-final local variables but can access final local variables.”
A local class can only access local variables that are declared final in java. When a local class accesses a local variable or parameter of the enclosing block, it captures that variable or parameter in java.


JVM create create a synthetic field inside the inner class in java -
As final variable will not change after initialization, when a inner class access final local variables compiler create a synthetic field inside the inner class and also copy that variable into the heap. So, these synthetic fields can be accessed inside local inner class even when execution of method is over in java.

You must be wondering, What are synthetic fields in java?
Synthetic fields are created by compiler and they actually doesn’t exist in source code in java.

Program to show Local Inner class cannot access the non-final local variables in java >
So, above program shows Local Inner class cannot access non-final local variables in java. Any attempt to access non-final local variables in Local Inner class will generate compilation error in java.


Program to show Local Inner class can access final local variables in java >
class OuterClass{
  
   //Inside this method we'll declare local class.
   void myMethod(){
         
          final int localVariable = 2; //Declare final local variable
         
          //Local Inner class
          class LocalInnerClass {
                 public void method() {
                      
                       System.out.println("inside local inner class's method > "
                               +localVariable);
                      
                 }
          } //End LocalInnerClass
         
     //Creating instance of LocalInnerClass
          new LocalInnerClass().method();
  
   }
  
}
/**
*  Main class, which will call local class via outer class instance
*/
public class InnerClassTest {
   public static void main(String[] args) {
     
      //Creating instance of OuterClass
      new OuterClass().myMethod();
     
   }
     
}
/*OUTPUT
inside local inner class's method > 2
*/

So, above program shows Local Inner class can access final local variables in java.


Summary >
So in this core java tutorial we learned What are local variables in java?
Where do local variables live in java? Where do object of local inner class live in java? What java docs says about “Local Inner class cannot access the non-final local variables but can access final local variables.” JVM create create a synthetic field inside the inner class in java. What are synthetic fields in java? Program to show Local Inner class cannot access the non-final local variables in java. Program to show Local Inner class can access final local variables in java.

No comments:

Post a Comment