• 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 would I use a method-local inner class?

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that you can declare a class inside of a method and that the method-local inner class can only access the local variables of the method which are marked as final.

I don't think I know of a case when I would want to use a method-local class.

Kaydell
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One common use of inner classes is as ActionListeners for Swing buttons, etc. This way, you can create a separate event handler for each button (or or menu or whatever), but have all of the code in one class (i.e., for the screen). Sometimes this is done with anonymous inner classes as well (i.e., where the inner class is created on the fly and not given a name).

An example of the first type:

aButton.addActionListener(new AButtonListener());
bButton.addActionListener(new BButtonListener());

class AButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("aButton pressed");
}
}
class BButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("bButton pressed");
}
}

Hope this helps. Mark Dexter
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is the least-often-used type of class in Java. At least for me. I don't recall if I've ever had a legitimate use for it, other than to test the concept. I think that when they came up with inner and nested classes, they just got the idea that they might as well let you declare a class anywhere. I suppose I might use a local class if I had a class that was small & simple enough to be an anonymous class, but I needed to use it more than once inside a method. Maybe I need to add a method to an existing class, then use the method. However I'm a big fan of keeping methods short, so it's hard for me to imagine a good example where I would use this. Probably I'd just make the class a member class instead.
 
Mark Dexter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim. I'm pretty new at Java and OOP, and I'm interested in your opinion. How would you do the mutliple button example? Would you create a separate class for each button? Thanks. Mark Dexter
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark. It occurs to me I may have misinterpreted the question. There are really two types of local classes: named local classes, and anonymous classes. Sun doesn't usually refer to anonymous classes as local classes, but they are - in the sense that they're classes defined locally inside a method (or constructor or initializer). But anonymous classes are shorter and have a different syntax. I interpreted the question as when would you use a named local class. And I usually wouldn't. I would use an anonymous class instead. In your example:

Generally I think that anything short enough to make into a named local class is short enough to make into an anonymous class. And anything too big to be an anonymous class is too big to be a named local class. So I use ananymous classes, and I use nested member classes (static and nonstatic), but I almost never use named local classes.
 
Mark Dexter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim. Thanks for the reply. It makes perfect sense to me. Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic