• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Help with inner classes.

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74042
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my example I have to, otherwise I have to drop Serializable from the implements list.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic