wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class usage" Watch "Inner class usage" New topic

Inner class usage

shyam prasad nandigam

Joined: Mar 11, 2009
Posts: 3
can any one give a real time example, where inner classes are exactly used...
Welly Tambunan
Ranch Hand

Joined: Aug 18, 2008
Posts: 40
I've just reading this article. I think this one si related to your question. Hope that's help

Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
Good Question. Generally inner classes are used to create anonymous local classes.For example someone wants to create a Thread object and pass a Runnable object in it.

The code is a bit complex to look at but you can create thread objects like this without even needing to implement Runnable.Another important usage of inner classes is in awt events. Inner anonymous classes come of great use while designing complex action events.

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Inner classes are also frequently used for adding action and event listeners, for example in a Swing application (e.g. a button). This then reduces the need for one action performed method to have to check which button was pressed and assigning the relevant action.

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Kavita Tipnis
Ranch Hand

Joined: Sep 21, 2008
Posts: 177
Inner classes can be used to do quite a great deal of stuff on the fly.
I wrote some quick code to see if they can be used to compare objects on the fly.
(Try it out as a mock question!)
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

thanks for contributing this example.

that should be:

on line 41

(it looks like the forum code alters it when I paste it in here.
here is the string I'm trying to paste:

Comparable comp=new Comparable<ArrayList<String>>(){ //creates an anonymous class which implements a comparable interface

here is how it looks after submission

Comparable comp=new Comparable<ArrayList><String>>(){ //creates an anonymous class which implements a comparable interface
Kavita Tipnis
Ranch Hand

Joined: Sep 21, 2008
Posts: 177
Yes, I tried to edit it but I could not.
shyam prasad nandigam

Joined: Mar 11, 2009
Posts: 3
thanks to one and all who reacted to my question and gave answers..
now i got a clear idea of inner classes and where they are used...
once again thank you very much to all
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Inner class usage
Similar Threads
What is a class file with $ symbol in the name
werid output for For loop
static inner class instantiation
Inner Classes
Inner Classes