Summary

In addition to a number of other items, a class definition can contain:
Member classes were explained in the previous lesson.  This lesson explains local classes.  Subsequent lessons will explain anonymous classes, as well as nested top-level classes and interfaces.

A local class is a class that is defined within a block of Java code. While local classes are probably most frequently defined within method and constructors, they can also be defined inside static initializer blocks and instance initializers.

An object of the local class must be internally linked to an object of the enclosing class (which I often refer to herein as the containing object).

A local class is truly an inner class because an object of the local class cannot exist in the absence of an object of the enclosing class.

The methods of a local class have direct access to all the members in the hierarchy of enclosing classes, including private members.  In addition, the methods of local classes have access to final local variables and final method parameters in the scope in which the local class is defined.

The containment hierarchy of local classes is independent of the inheritance hierarchy.  However, it is technically possible to establish an inheritance relationship between a local class and one of its enclosing classes.

Local classes may not be declared public, protected, private, or static.

Local classes cannot contain static members.

A local class has approximately the same relationship to a member class that a local variable in a method has to an instance variable of the class containing the method.
File: ea.htm [Next] [Prev]