Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with inner classes.

 
Joy Vergis
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please let me know the examples where we use local inner claaes, I am unable to think of any such scenarios where we can uuse local inner classes.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47260
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for inner ActionListener classes (on our GUIs forum) and inner Runnable classes, on GUIs, this forum and beginning Java. You will find local inner classes are used frequently.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that these will almost always be anonymous (local) inner classes. Named local inner classes are much rarer (I'm not sure I've ever seen one in a real application, and I'm pretty sure I've never used one). But I could imagine them being useful in a case where you want two similar instances of a local inner class - with an anonymous inner class you can only create a single instance (because there's no way to refer to the class afterwards).
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The collection framework has several named inner classes. For instance, Iterator implementations are often named inner classes instead of anonymous inner classes. Most of the time this is unnecessary though. If you only need an inner class in one location, make it an anonymous inner class. If you need the same instance multiple times in the same method, just assign the instance of the anonymous inner class to a variable:
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but they're usually named inner classes, not named local inner classes (at least, the ones I've checked).

As for the second part, that's why I said "similar", not "the same" . You might, for instance, want to parameterize it. Not that I ever have though.

 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right on both accounts. In fact, the only times I myself wrote local inner classes is when these classes needed to implement two interfaces, or extend a class and implement an interface. For example, a method I wrote to convert javax.swing.filechooser.FileFilter instances into java.io.FileFilter implementations*:
I wanted the returned class to not just implement java.io.FileFilter but also java.io.Serializable, so that the returned java.io.FileFilter is serializable if the given javax.swing.filechooser.FileFilter is.

* javax.swing.filechooser.FileFilter should already implement java.io.FileFilter - it already implements the only method required...
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in general i tend to prefer named inner classes over anonymous ones, but in the case of a local inner class it seems kind of like a fruit fly. it's life is so short why give it a name
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my example I have to, otherwise I have to drop Serializable from the implements list.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic