The moose likes Beginning Java and the fly likes nested vs. inner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "nested vs. inner" Watch "nested vs. inner" New topic

nested vs. inner

Stephen Suen
Ranch Hand

Joined: Oct 30, 2003
Posts: 34
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
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: nested vs. inner
It's not a secret anymore!