Witscale Test Center

4.6 Local inner class > 4.6.3 Accessibility of variables


4.6.3 Accessibility of variables

A method-local inner class has access to all member variables and methods of the outer class. Listing 4.6 illustrates how method-local class InnerOne can access members of the enclosing class.


 

You might think that whatever is accessible within a method body should also be accessible to method-local class. This assumption is partially true. Since a method can call other methods or access member variables of class, a method-local class can also do so. However, a very interesting thing about method-local class is that sometimes it cannot access the method’s local variables. Listing 4.7 illustrates how method local class cannot access method’s local variables.

 

 

 


The restriction that method-local class cannot access a method local variable is there for a reason. We have seen earlier that Java’s object memory model consists of two memory areas, the stack and heap. Java objects are created on heap, whereas reference variables, method local variables are created and stored on stack. Therefore, the local variable declared within a method are created and stored on the stack when the method is called. They live there only during the time when the method is executing. When the method ends, the method-local variables on the stack are destroyed and no longer accessible. On the other hand when a method-local class is instantiated, its instance is created on a heap and that instance may very well be alive (accessible) even after the method ends. For example, let us declare another member method getHelper() for OuterOne that declares a method-local inner class, instantiates it  and then returns its instance.

 

public class OuterOne {

 

  public Object getHelper(int b) {

   int a=8;

   class Helper{

     void accessibilityTest() {

      System.out.println(a);  //Error! cannot access method variable

      System.out.println(b); //Error! cannot access method parameter

     }

   }

   return new Helper();

  }

 

}

 

This getHelper() method creates a method-local class instance and returns its reference. The returns a reference to a Helper object. However, when the getHelper() ends, the method-variable a or parameter b is not alive. So, it should not be used in the Helper class. For this reason, the method-local class cannot use variables declared in method and parameters passed (if any). The method’s variables (including method parameters) are definitely not alive (accessible) after the method ends but the instance of method-local class may be.

However, there is one exception to the above rule. The method-variables marked as final can be used within a method-local class. Final variables hold a constant value and they are available even after the method is terminated, hence they can be used within a method-local class. Listing 4.8 explains how method local variables are not accessible in method-local class except as final variables.


 

MyHelper cannot access method local variable c and method parameter b. Method parameters are method local variables. Therefore, a non-final method parameter cannot be accessed in method-local inner class.