File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Anonymous versus Declared inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Anonymous versus Declared inner classes" Watch "Anonymous versus Declared inner classes" New topic
Author

Anonymous versus Declared inner classes

Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hello All, What is the advantage of using an anonymous inner class over a declared inner class?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I know of no technical advantages of one over the other as far as speed, resources used, etc, but for very small classes that are not reused, anonymous inner classes may make the code simpler and easier to read (for some) since the functionality of the class is held right where you're wanting to use it. Overuse and inappropriate use will have the opposite effect.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
It is quicker to write. If you are going to use objects of that class in several places, however, or similar objects, you are probably better of with a declared and named class (inner or top-level).
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Thank you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
You're welcome
 
 
subject: Anonymous versus Declared inner classes