• 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

When to use anonymous class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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......
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • 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:
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . only too pleased to be able to help.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic