• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inncer Class!  RSS feed

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,
can you please explain me with some examples why do we need inner class in java other than in eventlistener?
thanks.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*Need* would be too strong a word. Everywhere you are using an inner class, you could replace it by a top level class. In fact, to the JVM all classes are top-level - inner classes are just syntactic sugar fully handled by the compiler.
It's just that sometimes a class is so small and so tightly coupled to another class, that writing it as an inner class is simply much more convenient.
 
Chris Treglio
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I happen to have two of them in the class I'm working with right now ...
Here's a FileNameFilter

and here's a Comparator
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you should read Getting in Touch with your Inner Class .
It explains why lazy folks prefer Inner classes so that even I can understand it.
 
Chris Treglio
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Cindy that's harsh.
Do you really think that using anonymous inner classes like the ones I posted above are lazy programming? I'm open to suggestions, but I always thought that they were good examples of encapsulation .... the chance of being able to reuse one outside the class is pretty slim, especially the Comparator.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!