*
The moose likes Swing / AWT / SWT and the fly likes When to use anonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "When to use anonymous class" Watch "When to use anonymous class" New topic
Author

When to use anonymous class

Krishnamurthy Dara
Greenhorn

Joined: Jul 27, 2006
Posts: 7
Hi All,

I am developing a screen in applets which contains 5 Buttons. I need to handle all buttons action events. For this I have two choices to do.

1.Adding action listeners to all buttons and handling the events in single method called
public void actionPerformed(ActionEvent actionEvent) {} with five if else condition checking.

2.I can write actionPerfomed method for each and every button by using ActionListener() Anonymous Class.
eg: sendButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
submit();
}
});

Please suggest me which one is better in terms of different prospects like performance and coding standards......
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Performance: don't know.

If you search these fora, you will find differing opinions, some people favouring anonymous classes, others disliking them. Fortunately there are few people who recommend "addActionListener(this);"

I am one of those hwo like anonymous inner classes, but have a little rule of thumb:-
If your Listener does something completely different from all the other Listeners, then anonymous is easiest to code. Keep the code in the actionPerformed() method short; it might be better to call a method elsewhere in the class than to have a long actionPerformed() method.

If you have two Listeners which do the same thing, or several similar things, then you are better off with a named Listener class.

But I think the actionPerformed method with banks of if elses in is an abomination against good programming.

I have posted replies to similar questions many times, Here I was quite restrained.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18118
    
    8

Originally posted by Campbell Ritchie:
Performance: don't know.
I would go farther than that: you would have to do some seriously awful programming to make any noticeable difference, performance-wise, no matter what choice you made.

As for coding standards, if the coding standards at your place of work require (or forbid) inner classes then follow the coding standards.

And for the general advice, I agree completely with Campbell Ritchie.
Krishnamurthy Dara
Greenhorn

Joined: Jul 27, 2006
Posts: 7
Thank you very much Campbell and Paul for your reply. Hope there is no much difference in Perfomance also..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
. . . only too pleased to be able to help.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Krishnamurthy Dara:
Hope there is no much difference in Perfomance also..


The difference should be at an order of magnitude of nano- to micro-seconds.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to use anonymous class
 
Similar Threads
Inner class
Inner classes and Mediator Pattern
Trying to figure out the "This" keyword
problem with parse int method
Performance of Anonymous class