wood burning stoves 2.0*
The moose likes Java in General and the fly likes Inner classes Types Problem 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 » Java » Java in General
Bookmark "Inner classes Types Problem" Watch "Inner classes Types Problem" New topic
Author

Inner classes Types Problem

Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi Friends...
Could U pls clarfy some of doubts abt the "Inner classes".
Though know types of inner classes
class inside the class
class insde the method static class
anonomous nner classes..
But i do want real time example like situation where we can use all these...

Y Swing event handlers mainly uses the "Anomous classes" rather than another ..concept...Is there any specific reason
[ July 24, 2005: Message edited by: Gowtham Ganamukala ]

Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Barend Garvelink
Ranch Hand

Joined: Dec 07, 2004
Posts: 64
Please be more verbose, I can hardly understand what you're asking for, exactly. Inner-classes are a bit intricate, but they're very well covered in the Certified Java Programmer study books, so you might want to read the chapter on inner classes in one of those.

Basically, and inner class is a class defined inside some other class (but you knew that). Inner classes are able to access the private members of their outer class instance.

A static inner class does not have this ability to access the privates of his outer class. This makes a static inner class very much like a regular class, and in many cases, using one of those mainly adds confusion over using a regular class without giving any real benefits.

Anonymous inner classes are often used because they save you having to explicitly define a class type. It's a shorthand, designed to save time while writing the code. Again, use with caution, cause they can make the code hard to understand.

As for the "real time examples" you've so kindly demanded, I have little inclination of giving them. You didn't say please, and you don't seem to realise any answer you get to your questions is a favour from someone whose time is at least as valuable as yours. If the answer above doesn't help you, I recommend the SCJP study guides.


SCJP 1.4, SCJD 1.4, SCWCD 1.3, ICSD:Websphere 5.1
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi Barend ,
Thanks for your reply.

I just understood how exactly the code of 4 types of Inner Classes goes.

Could U please explain or just a link Y "events handlers in java" heavily uses the "annomous inner classes" .

Please suggest cause i cant able to decipher where these are used though i know the implementation
Barend Garvelink
Ranch Hand

Joined: Dec 07, 2004
Posts: 64
I think they are mostly used for the convenience of whoever wrote the code. An anonymous inner class is declared right then and there on the spot where you're sending it into some addWhateverListener() method. A regular innerclass has to be defined someplace else in the sourcefile and then has to be newed into place inside the method.

I think it's mostly a matter of personal preference of the coder, there's no "law" one way or the other -- of course, if you have to add the same event handler to multiple sources then it's easier to declare it just once as a regular innerclass.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes Types Problem
 
Similar Threads
inner extends Object
Extending inner class
Inner classes
Anonymous Inner Class
Inner class basics. (Marcus Green's mock exam 2)