Witscale Test Center

4.7 Anonymous inner class > 4.7.4 Accessibility of variables


4.7.4 Accessibility of variables


Accessibility of variables for anonymous class is very similar to that of a method-local class. Anonymous class defined inside a method has access to final method variables and to outer class's member variables. If anonymous class is defined in a code block, it has access to block variables, that are declared as final as well as the outer class's member variables. Listing 4.9 illustrates the variables accessible to the anonymous class declared inside the goBiking() method.

 

 

Anonymous class defined in goBiking() method has accessibilityTest() as its member method to show you what all it can access. This class has access to final method-variable distance and the outer class's member variables.

Tip: Anonymous class also has access to inherited members of its super class. For example, the anonymous class in listing 4.9 can access the class Bicycle’s member methods paddle() either by calling super.paddle() or simply paddle().