• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Anonymous Class

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
so far I've been thinking that anonymous classes are easy and better to use for implementing any listener interfaces. But, I have recently read in an article that:
"...The example code above uses anonymous classes for simplicity in understanding but practically anonymous classes should not be used for listeners."
What are the side-effects of using any anonymous class?
Thanks,
Kalyan.
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method actionPerformed in the inner class can only access final variables in the enclosing class. I normally call another method in the enclosing class from my inner class's method.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit of clarification: an anonymous inner class can only access final local variables in the enclosing method, but has access to all of the enclosing class's member variables.
As far as the article quote, I don't think you'll find many people who would agree. Most people feel that anonymous inner classes are absolutely perfect for short listener classes. If you need a listener to do a lot of complicated stuff, you should consider either making the listener its own top-level class, or use an anonymous listener that just delegates to methods in the enclosing class.
You can't believe everything you read
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are no side-effects per se, but some potential drawbacks of anonymous classes are:
  • Difficult to reuse;
  • Difficult to test;
  • Can make methods hard to understand (especially if the code is badly formatted).


  • The advantages are:
  • A bit less typing;
  • No other methods can use the class (so, you can muck around with it as much as you like and it won't affect anything else);
  • Fewer classes (less clutter).

  •  
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Since most GUI type applications have a lot (italicised to denote that it is a vague measurement) of listeners attached, there can be a significant performance penalty if anonymous classes are used since the class loader must load a lot of classes that it would not have had to do otherwise.
    This is just another one of those "performance versus code maintenance" issues that I see quite often. Personally, I believe going one way or the other is perfectly fine, as long as it is a conscious and informed decision (in contrast to a naive decision), and better still, the decision and reasoning is documented.
    More often than not, I choose code maintenance over performance, since performance is much cheaper than maintenance i.e. it's generally much cheaper to buy a faster CPU/more memory than it is to spend hours trying to read/fix/maintain code.
     
    Kalyan Mudumbai
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks people, for your inputs.
    Tony, you have mentioned:
    "Since most GUI type applications have a lot (italicised to denote that it is a vague measurement) of listeners attached, there can be a significant performance penalty if anonymous classes are used since the class loader must load a lot of classes that it would not have had to do otherwise."
    Could you please elaborate on this? Why would the class loader load the anonymous classes even if they are not required? Please throw some light on this.
    Thanks,
    Kalyan.
     
    Ranch Hand
    Posts: 1923
    Scala Postgres Database Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I guess he meant that you could reuse some of the listeners, if they weren't anonymous.
     
    The knights of nee want a shrubbery. And a tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic