Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
shweta agarwal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use them when instantiating TreeMap with a sort algorithm.

Pho
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic