| Author |
Different between inner-class and top level nested class :o
|
Francis Siu
Ranch Hand
Joined: Jan 04, 2003
Posts: 867
|
|
I have no idea between inner-class and Top level nested class Can anybody answer this question? thank you for your attention
|
Francis Siu
SCJP, MCDBA
|
 |
Greg Charles
Bartender
Joined: Oct 01, 2001
Posts: 1855
|
|
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!
|
 |
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
|
|
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 ]
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
|
|
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.
|
"I'm not back." - Bill Harding, Twister
|
 |
Francis Siu
Ranch Hand
Joined: Jan 04, 2003
Posts: 867
|
|
Thanks for Greg ,Peter and Jim Greg 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 Peter 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!
Jim Is the terminology of "static inner class" universal? thank you for your respond
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
|
|
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.
|
 |
Francis Siu
Ranch Hand
Joined: Jan 04, 2003
Posts: 867
|
|
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
|
 |
 |
|
|
subject: Different between inner-class and top level nested class :o
|
|
|