Forums Register Login
Different between inner-class and top level nested class :o
I have no idea between inner-class and Top level nested class
Can anybody answer this question?
thank you for your attention
My Java in a Nutshell talks about nested top-level class in the same chapter as inner classes, but says it is not really an inner class itself. Its closest relative is the member class, which is definitely an inner class, so I'll try to explain how they differ.
A nested top-level class is declared with the static keyword, while a member class isn't. (Really helpful, huh?)
A nested top-level class acts just like a regular class. The fact that is defined inside another class is just for organizational convenience. You can instantiate and use objects from it anywhere you want. You just have to qualify the class references:

A member class is more like member variables and methods. You can instantiate it, but the objects can only exist inside instances of the enclosing class. As a tradeoff, member classes have access to all the other members, public and private, of the enclosing class.
I remember when Java 1.1 came out, I read the explanations of the various inner classes and just went cross-eyed. In the ensuing six years, the concepts have sunk in somewhat. I hope it doesn't take you so long!
Beware. The confusion that Greg was talking about is sometimes caused in part by the fact that not everyone gives the term "inner class" the same meaning. Even in the articles and other documentation on the Sun website itself you can find two different definitions being used!
The first definition is the one outlined by Greg. It is used in the Java language specification itself.
However, in some places any nested class is called an "inner class". In those cases, you can find people referring to a "static inner class" which would be an oxymoron according to the first definition.
If you ask me, we should just stick to the definition used in the JLS. But you should still be aware of these awkward differences because otherwise some books and articles may confuse you no end!
- Peter
[ April 05, 2003: Message edited by: Peter den Haan ]
Of course, people using alternate definitions are just plain wrong, and should be ignored. Even if they're from Sun.
Also, it's worth noting that the term "top-level nested class" has been invalid since JLS 2nd edition came out. This term was used in the original Inner Classes Specification, but it's since been dropped. (Fortunately, since it was a very poor choice of terminology which caused much confusion.) A class is either top-level or it's nested; it cannot be both. The standard replacement term is "static member class" - though "static nested class" is also OK. People using the term "static inner class" should be shot however.
Thanks for Greg ,Peter and Jim
I worth to spend more time to read your message
because it is useful for me to understand it
thank you for your time to reply
Thank you for your advice

But you should still be aware of these awkward differences because otherwise some books and articles may confuse you no end!

Is the terminology of "static inner class" universal?
thank you for your respond
Is the terminology of "static inner class" universal?
No. According to the JLS, it's completely wrong - a nested class that's static is not an inner class, by definition. It's just a static member (or static nested) class. However there are a number of books out there which use the term, incorrectly. Some of these books still contain valuable and useful information, if you ignore the bad terminology. Whenever you see the words "static inner class", just pretend they said "static member class". Don't contribute to the problem by using the term yourself.
hi Jim
thanks for your details description of it
now, I know the definition between static member class and inner class.
I wonder if you can give me some examples to know more
thank you for your time

This thread has been viewed 1982 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 11, 2018 22:29:42.