Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Why anonymous inner class?

 
John Le
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48436
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
peter cooke
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Report post to moderator
You need to create an adapter pattern object for an instance of some some class.
See Adapter Pattern........
 
Campbell Ritchie
Sheriff
Posts: 48436
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 48436
56
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank you very much. 240V RMS in Britain, 220V RMS in most of the rest of Europe.
 
    Bookmark Topic Watch Topic
  • New Topic