• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class usage

 
shyam prasad nandigam
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one give a real time example, where inner classes are exactly used...
please
 
Welly Tambunan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Kavita Tipnis
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I tried to edit it but I could not.
 
shyam prasad nandigam
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic