More than one Java author that I have read, when they talk about the short history of Java, say that anonymous inner classes were invented in great part to facilitate the Java 1.1 event handling in the AWT. I am moving this thread to the Java beginner section where it will get more attention.
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Joined: Jan 30, 2000
Being lazy, I got this non-AWT example from the Nested Classes Specification here: Consider a method myEnumerate() which is responsible for returning an Enumeration of the elements in an array. (Look up Enumeration if you're not familiar with it.) This could be done using a local class as follows:
This can also be done using an anonymous class which does the exact same thing:
Study the differences in syntax carefully. Note that since an anonymous class has no name, it can only be used once, to create exactly one instance of itself. (How can you refer to it again since it has no name?) But if that's not a problem, it can be somewhat shorter to write than a local class.