• Post Reply Bookmark Topic Watch Topic
  • New Topic

inner classes  RSS feed

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone.

how are inner classes used?
what are its practical applications or in what instances or situations that might need the use of inner class?

thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A practical example would be listeners in Swing.
Look at Sun's tutorial
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterators are another good use for inner classes. Since the inner class has access to all of the outer class data it makes it easy for the inner class to inspect all of the data. In this way the inner class is acting a lot like a "friend" class from C++.

_M_
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the JavaRanch campfire story, "Getting in touch with your inner class."
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anonymous inner classes are commonly used for event handling in GUI programming and when creating threads. Named inner classes are typically used when there is a tight connection between two classes. As mentioned above, iterators are one common example of this.

Layne
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.

i did read getting in touch with your inner class.
so inner classes are mostly used in swing applications? do they have practical applications in web systems?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shuini gustav:
thanks.

i did read getting in touch with your inner class.
so inner classes are mostly used in swing applications? do they have practical applications in web systems?


Try to understand *why* inner classes are popular in Swing apps. Is there something inherently "Swingy" about them? No. Then there must be some conditions in Swing apps that make them an appropriate solution.

To break down inner classes further, anonymous inner classes are useful when you need to implement a short class (larger anonymous classes smell), and it is dependent on its enclosing class -- you don't feel compelled to recast it as a top-level class. In Swing, many event listeners fall into this category. Does this same situation arise in web apps? Depends on your framework/design.

As for member classes, they again reflect a dependence on their enclosing class, they can be larger, and we are using nesting to either hide them from other classes (often with private- or package-level access) or use the nesting to add structure to the classes in a package. Using this technique seems to be a matter of taste -- I don't see a particular affinity between members classes and Swing apps per se. For an example of this in the collection framework, consider collection iterators. What classes in the API implement java.util.Iterator? No really key classes. So collections like ArrayList and the key and value collections of HashMap must either be using nested classes or non-public classes to implement iterators. This leaves the collection framework cleaner, because fewer concrete classes are exposed.
[ January 12, 2006: Message edited by: Jeff Albrechtsen ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!