jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes What is this technique called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is this technique called?" Watch "What is this technique called?" New topic

What is this technique called?

Charles Mulloy
Ranch Hand

Joined: Oct 25, 2009
Posts: 30
A while back, I was extending ActionListener but for reasons I can't fathom it stopped compiling right. I looked around and found the following method.

I don't know what this technique or concept is called, but I want to learn more about it, instead of just copy, pasting, and modifying it without understanding the mechanics behind it. Anyone know what I'm talking about?
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

It is called an anonymous inner class

Regards, Rene Larsen
Dropbox Invite
Ramon Gonzalez

Joined: Jul 26, 2010
Posts: 2
Why would someone use this?

Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

It is very helpful if you e.g. want to do a sort on a List that will be showing on a web page.

e.g. like this:

This is just one way of using it.

When you are coding Swing applications, you almost NEED to use these inner classes when coding an ActionListener.
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
Ramon Gonzalez wrote:Why would someone use this?

You use it when all you need the class to do is one thing and have just one object....

Eg you have an interfface or an abstract class. All you want to do is override that method once and use it. Thats where you use anonymour classes. try googling anonymous classes for more info.
swapnil kachave

Joined: Feb 10, 2010
Posts: 27
Ramon Gonzalez wrote:Why would someone use this?

Anonymous class is class which doesn't have name. This is useful when you want some functionality is specific to some classes such as in given example the Listener method which you are implementing is not shared by another class it is totally or more specific it is made for only to the that specified object that time anonymous class is useful.
Lester Burnham

Joined: Oct 14, 2008
Posts: 1337
More generally, this technique is called a "callback", since it defines a method that is called back by the application at some later point in time. In Java, anonymous inner classes are used to implement it, but the technique itself is much older than that, going back at least to X-Windows.
I agree. Here's the link: http://aspose.com/file-tools
subject: What is this technique called?
It's not a secret anymore!