Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use anonymous class

 
Krishnamurthy Dara
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20718
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Campbell and Paul for your reply. Hope there is no much difference in Perfomance also..
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . only too pleased to be able to help.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic