difference is that once a local variable or parameter has been hidden it is impossible to refer to it.
5.3.1. Inner Classes in Static Contexts
We stated that an inner class is usually associated with an instance of the enclosing class. It is possible to
declare a local inner class, or an anonymous inner class (see next section), in a static context: within a static
method, a static initialization block, or as part of a static initializer. In these static contexts there is no
corresponding instance of the enclosing class, and so the inner class instance has no enclosing class instance.
In these circumstances, any attempt to use a qualified-this expression to refer to an enclosing class's
instance fields or methods will result in a compile-time error.
5.4. Anonymous Inner Classes
When a local inner class seems too much for your needs, you can declare anonymous classes that extend a
class or implement an interface. These classes are defined at the same time they are instantiated with new. For
example, consider the walkThrough method. The class Iter is fairly lightweight and is not needed outside
the method. The name Iter doesn't add much value to the codewhat is important is that it is an Iterator
object. The walkThrough method could use an anonymous inner class instead:
public static Iterator
return new Iterator
Anonymous classes are defined in the new expression itself, as part of a statement. The type specified to new
is the supertype of the anonymous class. Because Iterator is an interface, the anonymous class in
walkThrough implicitly extends Object and implements Iterator. An anonymous class cannot have
an explicit extends or implements clause, nor can it have any modifiers, including annotations.
Anonymous inner classes cannot have explicit constructors declared because they have no name to give the
constructor. If an anonymous inner class is complex enough that it needs explicit constructors then it should
probably be a local inner class. In practice, many anonymous inner classes need little or no initialization. In
either case, an anonymous inner class can have initializers and initialization blocks that can access the values
that would logically have been passed as a constructor argument. The only construction problem that remains
is the need to invoke an explicit superclass constructor. To solve this problem the new expression is written as
if a superclass instance were being constructed, and that constructor is invoked as the superclass constructor.
For example, the following anonymous subclass of Attr (see page 76) invokes the single-argument Attr
constructor and overrides setValue to print out the new value each time it is changed:
Attr name = new Attr("Name") {