• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested Classes: When/Why Use? Static vs non-static?  RSS feed

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen tons of classes written with a static or non-static nested class, as well as package-level access classes included in another class' ".class" file. However, I have never been able to see how those implementations offer a benefit over having the static/non-static nested/inner class be in its own file, and not be nested. I know that in a nested class you can use private variables of the enclosing class, but you could pass those anyways (which to me seems cleaner). So why nest? And why static vs. non-static?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,
I guess it really comes down to reuseability of a class and what is the purpose of the class. For example if you have a class that extends Canvas and whose sole purpose is to animate a frog leaping across a FrogLeapFrame then it really makes more sence to make the Canvas an inner class since it is specific to the FrogLeapFrame and chances are we'll never find another use for the Canvas. As for private, package, public or protected inner classes, I am of the opinion that only private should be used for inner classes unless the the class is static or you plan on subclassing the main class and will have use for the inner class in the subclass. If the class has either package or public scope why not define it in a separate file? Static inner classes are for use in methods that are static (since they cannot access any identifier that is not static) or as a cheap and dirty package extension.
Michael Morris
[ February 10, 2003: Message edited by: Michael Morris ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use inner classes most commonly with event handling in Swing. For example,

I find this useful since you don't have just one ActionPerformed() method with a long if statement for several buttons.
I don't know how to compare this with static inner classes or top-level classes with package access. It's just an example of where anonymous inner classes can be useful.
HTH
Layne
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for both the replies.
To the question of public/package inner classes: yeah, I agree, but i see it a lot! So I was wondering if I was missing something.
To the idea of anonymous classes created for adding listeners, runnables, etc - sorry, I wasn't clear. That wasn't what I was asking about. I agree those are useful, but I'm talking of named, non-anonymous classes that are created in a class definition.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with everything LL and MM have said above, except: there is no such thing as a static inner class. Inner classes are inherently non-static by definition. The correct term is static member class or static nested class. In the past "top-level nested class" was also considered correct (and was even the preferred official term), but this linguistic abomination was removed in JLS 2nd edition - "top-level" and "nested" are contradictory.
Incidentally a good example of a public static member class (well, interface really) is Map.Entry in java.util.Map. It's not useful in any context other than when you're using a Map, so it makes sense to have it defined as part of the Map interface. (Especially since it's also very short & simple.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!