• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Where are Inner Classes used (except for the SCJP test)?

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where exactly are we going to use inner classes? I've looked through a good bit of the API and haven't seen one place where inner classes were used. I don't have any real world experience, so maybe you use inner classes in every program on the job, I don't know. I just fail to see where using an inner class is the best way to go.
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
allright, I have to clarify a little. I know from experience that anonymous classes come in very handy, but aside from those.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
everytime instead of making a class outside the main class and then making an object to access its functions , you can use inner classes.
for eg: inner classes can be very effectively used when u write codes using event handling.
eg2: while using GrifBag layout manager.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to the two uses above, I also use them alot when I'm using threads... you get all the advantages of a Runnable object ( i.e. local attribute and method calls ) , but you can have more than one run() method...

-Nate
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would use an inner class anytime you wanted to hide a class from everyone else.
I have not used them a lot but I ran into one example in which they were helpful. I had a class on which I wanted to keep some simple statistics. I wanted all the statistics code to live in its own class, but didn't want anyone else to use this statistics class. So I made this stats code an inner class.
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use them when instantiating TreeMap with a sort algorithm.

Pho
reply
    Bookmark Topic Watch Topic
  • New Topic