| Author |
Why anonymous inner class?
|
John Le
Greenhorn
Joined: Dec 18, 2008
Posts: 4
|
|
Every once in awhile I come across an anonymous inner class, especially for comparators.
My question is why bother with this type of class? What do I get out of using it? Is the only reason to avoid a bunch of extra java files? or a bunch of extra classes in a single java file? Don't they all get compiled to class files? Even the anonymous inner class?
I just don't see the utility in using it.
Can anyone explain it to me? Or point me to an article that tells me why I should use them?
Thanks
|
 |
Sunil Kumar
Ranch Hand
Joined: Apr 24, 2007
Posts: 76
|
|
The use of anonymous class in my view comes only in following scenarios
- Only one Instance required
- Used right away when defined
- Very Short body
In essence it does two steps of defining and instantiating a class in one step when only one instance is required. Further, its scope is the method or code block where it is defined, after which it just vanishes.
Sometimes it is said that an anonymous class can be used to instantiate an interface but in my view it is the anonymous implementation of the interface
|
Sunil Kumar
http://goodtoknowit.blogspot.com/
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32674
|
|
Those scenarios are actually quite common; in Swing you get buttons with one action each, which can be supplied easily with an anonymous implementation of the ActionListener interface. You can also implement Runnables to start threads off.
And surely an implementation of an interface is called a class . . .
It is useful when you are implementing something once only (agree you should never write the same anonymous class twice) to have the implementation closely associated with its use.
Can't think of an article offhand, I am afraid.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24056
|
|
Note that the anonymous class -- like all inner classes -- has access to the member variables of the class it's embedded in; this makes the inner class simpler than it would be if you made it a separate top-level class.
But in addition, the anonymous class has access to any final local variables in the method, and that's what makes them really convenient. If you define a class right inside the only method that uses it, then that class doesn't explicitly need a constructor to move local variables from the method to the class; the variables are accessible without any effort on your part. This usually makes an anonymous inner class smaller and clearer than if you wrote the same class in any other way.
|
[Jess in Action][AskingGoodQuestions]
|
 |
peter cooke
Ranch Hand
Joined: Mar 16, 2004
Posts: 310
|
|
You need to create an adapter pattern object for an instance of some some class.
See Adapter Pattern........
|
CIAO Peter M. Cooke
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32674
|
|
peter cooke wrote:You need to create an adapter pattern object for an instance of some some class.
See Adapter Pattern........
Please explain a bit more. Remember this is beginners' and many readers will not have heard of the Adapter Pattern.
|
 |
peter cooke
Ranch Hand
Joined: Mar 16, 2004
Posts: 310
|
|
The adapter pattern is to provide a functionality like a power plug adapter for your electronics. In the US we use 110V (rms) or 115V peak to peak. In the UK they use a different plug and a different voltage. (sorry I don't remember what it is). You need an adapter.
An example that I had to use the adapter pattern: I had a very specific (known / non-varying) collection of java objects that needed to be copied out to a highly controlled validated XML document. I wanted a single method that put the representation into the XML tree for all of the java objects. I created anaonmous inner class adapters for each java object in group and passed them to the single write method.
Could I have chosen another way of doing things. Probably. I read about using anonmous inner classes as adapters and wanted to try it in something that was not critical.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32674
|
|
|
Thank you very much. 240V RMS in Britain, 220V RMS in most of the rest of Europe.
|
 |
 |
|
|
subject: Why anonymous inner class?
|
|
|