aspose file tools*
The moose likes Java in General and the fly likes Help with 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 » Java in General
Bookmark "Help with inner classes." Watch "Help with inner classes." New topic
Author

Help with inner classes.

Joy Vergis
Ranch Hand

Joined: Sep 14, 2009
Posts: 45
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Apr 06, 2010
Posts: 4366
    
    8

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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

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

Joined: Oct 27, 2005
Posts: 19672
    
  18

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

Joined: Oct 21, 2000
Posts: 4340
    
    2

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


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

In my example I have to, otherwise I have to drop Serializable from the implements list.
 
jQuery in Action, 2nd edition
 
subject: Help with inner classes.