| Author |
Anonymous class --simple example
|
josephine chen
Ranch Hand
Joined: Oct 29, 2002
Posts: 216
|
|
|
I neeed an simple example without involving awt.
|
 |
Tony Alicea
Desperado
Sheriff
Joined: Jan 30, 2000
Posts: 3219
|
|
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
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
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.
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: Anonymous class --simple example
|
|
|