• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is this technique called?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is called an anonymous inner class
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would someone use this?
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic