This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes 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 "Inner Classes" Watch "Inner Classes" New topic
Author

Inner Classes

Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi,

Can anyone tell what is the use /advantages of having Inner classes.Tell me a real world scenraio when we may need them.

Regards,
Vineela
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Common use: as event listener classes in Swing GUIs.

Advantage: listeners are often small classes that know implementation details of their enclosing classes. This is a good match for nested classes.

Disadvantage: code reuse. But when you realize a listener can be turned into a useful top-level class (for example the Action behind the exit menu item), you refactor. Most of the time event listeners are too specific for you to consider reuse.


There is no emoticon for what I am feeling!
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
creating a class that implements the Comperator interface can be done on the fly as a anonymous inner class. that can be by far the easiest solution if you need to pass a Comperator into a method call, even though i go with poster #1 - the code is not so reusable...

jan
 
wood burning stoves
 
subject: Inner Classes