File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class usage" Watch "Inner class usage" New topic
Author

Inner class usage

shyam prasad nandigam
Greenhorn

Joined: Mar 11, 2009
Posts: 3
can any one give a real time example, where inner classes are exactly used...
please
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

http://www.javaspecialists.eu/archive/Issue002.html
Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
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.


regards,
Arka
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
Greenhorn

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class usage