File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is this technique called?

 
Charles Mulloy
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is called an anonymous inner class
 
Ramon Gonzalez
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would someone use this?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1337
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic