hi all, I'm wondering how I can make a choice between nested classes and inner classes, particularly when both implementation are really applicable. Considering for the hiding, efficiency and other factors, is there any difference between two implmentations? Thanks for any advise Stephen Suen
posted 11 years ago
All inner classes are also considered nested. I'm guessing you're asking if it's better to use inner classes or static nested classes. I'd say that if you don't need to access an outer "this" reference then static nested is simpler, and easier for others (especially beginners) to understand. But if you have a legitimate use for an outer this reference, go ahead and use an inner class instead. In other words, use "static" in the class declaration unless and until your compiler complains, then change it if necessary. Alternately if your (inner/nested) class is really short and simple, you might just want to declare it as an anonymous class instead - which is a kind of inner class, not static. Anonymous classes can be relatively compact, which is nice if the class is really simple.