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
walkThrough(final Object[] objs)
{

return new Iterator()
{
private int pos = 0;
public boolean hasNext()
{
return (pos <>
}
public Object next() throws NoSuchElementException
{
if (pos >= objs.length)
throw new NoSuchElementException();
return objs[pos++];
}
public void remove()
{
throw new UnsupportedOperationException();
}
};
}



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.
Attr constructor and overrides setValue to print out the new value each time it is changed:

Attr name = new Attr("Name")
{
public Object setValue(Object nv)
{
System.out.println("Name set to " + nv);
return super.setValue(nv);
}
};



In the Iterator example we invoked the no-arg superclass constructor for Objectthe only constructor that can ever be used when an anonymous inner class has an interface type.

Anonymous classes are simple and direct but can easily become very hard to read. The further they nest, the harder they are to understand. The nesting of the anonymous class code that will execute in the future inside the method code that is executing now adds to the potential for confusion. You should probably avoid anonymous classes that are longer than about six lines, and you should use them in only the simplest of expressions. We stretch this rule in the walkThrough example because the sole purpose of the method is to return that object, but when a method does more, anonymous classes must be kept quite small or the code becomes illegible. When anonymous classes are used properly, they are a good tool for keeping simple classes simple. When misused, they create impenetrable inscrutability.


We Are Founder..