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 ]
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.
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
Joined: Dec 07, 2004
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.