wood burning stoves 2.0*
The moose likes Java in General and the fly likes Anonymous Classes : Significance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Anonymous Classes : Significance" Watch "Anonymous Classes : Significance" New topic
Author

Anonymous Classes : Significance

Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
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


Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Thats just terrible. A class should try to be a single object. Not multiple different ones.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

Joined: Oct 05, 2001
Posts: 1170

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
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
No, a class is not an implementation of an object. An object is an implementation of a class.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

Joined: Jul 15, 2003
Posts: 1078
I believe he was referring to the Java Tutorial which implements the interfaces as part of the component.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

    Joined: Oct 05, 2001
    Posts: 1170

    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

    Joined: Jul 15, 2003
    Posts: 1078
    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

    Joined: Oct 05, 2001
    Posts: 1170

    You too?
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Anonymous Classes : Significance
     
    Similar Threads
    Using A-D.A.I.C.
    Static inner class inside Interface!!
    Extending inner class
    Nested classes
    enum within a method?