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?
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 ]
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
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
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.
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.)
"I'm not back." - Bill Harding, Twister
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad: