This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Hi,
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.
Ex:
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.
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()!
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!)
(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
posted
0
Yes, I tried to edit it but I could not.
shyam prasad nandigam
Greenhorn
Joined: Mar 11, 2009
Posts: 3
posted
0
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