• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner class usage

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one give a real time example, where inner classes are exactly used...
please
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!)
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic