• 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

Anonymous Classes : Significance

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

Can anyone tell me what is the significance of using Anonymous inner classes i.e. why are they used?

It will be great if someone can elaborate it with example and syntax.

Thanks in advance,
Saurabh
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I write about this regularly on these fora, and as you will see I have my own ideas about anonymous classes. I use them all the time. A couple of threads I found, here, and this thread, which gave rise to this second thread in reply.

As I said, the commonest anonymous inner class is probably the ActionListener, but anonymous Runnables are by no means rare. I think is is very unfortunate that the Java tutorial uses addActionListener(this) passim.

CR
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
I think is is very unfortunate that the Java tutorial uses addActionListener(this) passim.



Indeed. Is there anything in the Java Tutorial about *good* design? They should at least note this is just an expedient.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff Albertson, I see you and I agree on that point. I don't remember there being much about class design in the Java tutorial, no.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats just terrible. A class should try to be a single object. Not multiple different ones.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A class should try to be a single object. Not multiple different ones.

A class can't be any number of objects. A class is a class, but it may be instantiated to give rise to objects. And unless it is a Singleton type, the "client" classes can instantiate as many objects from it as they like.

But it is permissible for a class to include other classes inside its body. There are two common kinds: the inner class with a name, and the anonymous inner class.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean a class can be instantiated to give rise to instances. A class is an implementation of an object. An object is defined by its contract which for java is basically its 'type.'

When a class implements multiple interfaces it is attempting to represent multiple objects.

-EDIT-
To be clear, when I say object I am refering to a design-time concept. What we conceptualize as objects. A class should not represent multiple conceptual objects.
[ June 16, 2006: Message edited by: Mr. C Lamont Gilbert ]
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, a class is not an implementation of an object. An object is an implementation of a class.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He was pretty clear that by object he did not mean an instance of any class, he specifically meant the conceptual "object" at design-time which exists before there is a class or any implementation actually written. Ideally an object should do one thing and one thing only. When a class is written with multiple interface inheritance it is generally a sign of the object trying to implement unrelated behavior, attempting to do more than one thing, or it is a sign of a poorly written interface heirarchy where the behavior is divided in ways it probably shouldn't be.

I don't think he is talking about something like a Set, which inherits from Collection. Rather he might be talking about an implementation that is both a List and a Set. I don't really see any problem with that, it's far more desirable than creating a ListSet interface instead.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree that an object (as does the class it is instantiated from) represents one thing and one only. But what I meant originally is that inner classes are permissible because they produce separate objects, which are distinct from their enclosing class.

I have a suspicion that we actually might think the same thing, but are expressing it differently.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe he was referring to the Java Tutorial which implements the interfaces as part of the component.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, if he was referring to addActionListener(this) . . .

  • Sorry for mouthing off, I was wrong to do that.
  • We are in full agreement that in most cases addAnythingListener(this) is an abomination against good programming.

  • There is one instance where addAnythingListener(this) is good practice. that is where the listener actually refers to the state of the Component. If you want to do anything with the position of the mouse pointer, for example, then you would want the MouseListener in the Component.
     
    Mr. C Lamont Gilbert
    Ranch Hand
    Posts: 1170
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Campbell Ritchie:
    Oh, if he was referring to addActionListener(this) . . .

  • Sorry for mouthing off, I was wrong to do that.
  • We are in full agreement that in most cases addAnythingListener(this) is an abomination against good programming.

  • There is one instance where addAnythingListener(this) is good practice. that is where the listener actually refers to the state of the Component. If you want to do anything with the position of the mouse pointer, for example, then you would want the MouseListener in the Component.


    Which leads us back to the original question. That is a good opportunity to use an inner class. Personally I have stopped using anonymous inner classes as much, but its no big deal really.
     
    Ken Blair
    Ranch Hand
    Posts: 1078
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeah, I actually prefer static nested classes or in some cases inner classes when implementing listeners. My single biggest use of anonymous classes is passing a Runnable, especially in anything related to the GUI where I am constantly moving work between threads to keep the GUI responsive.
     
    Mr. C Lamont Gilbert
    Ranch Hand
    Posts: 1170
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You too?
     
    Attractive, successful people love this 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