This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
[Jesper]: "Nested top-level class", that's a contradiction. A class is either a top-level class or a nested class, but cannot be both at the same time.
To be fair, when Sun first introduced nested classes to Java, they defined things a bit differently, and "top-level nested class" was the official Sun-sanctioned term for what we now call a static nested class (or static member class). This was a horribly nonsensical choice of terminology, and in 2000 the Java Language Specification 2nd Edition came out and removed this particular term. Not everyone realizes this though, and some people, sadly, continue to refer to top-level nested classes. Of course, there are also people who refer to "static inner classes", but those people were always wrong. Either way, the correct term now is static nested class, or static member class.
"I'm not back." - Bill Harding, Twister
Joined: Oct 31, 2006
Thanks for the reply an explanations .
Sun still refers static nested classes as top-level inner classes. My latest SCJP book (KS & BB,1.5) has this term.
But there are these nested subclasses . I have seen the same example of Point2D on internet but could not understand properly.
Rob , can you explain how exactly it differers from regular nested classes with simple example ? Or can you send me some link where it is explained in detail?
Thanks for the help, Vivian
Joined: Jan 30, 2000
[Vivian]: Sun still refers static nested classes as top-level inner classes. My latest SCJP book (KS & BB,1.5) has this term.
OK, but KS & BB are not Sun. The JLS now has fairly clear, straightforward definitions of these words: "A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class." There is no overlap between the two. (Anymore.)
Originally posted by Vivian Josh: Rob , can you explain how exactly it differers from regular nested classes with simple example ? Or can you send me some link where it is explained in detail?
The example I showed you should never be used
It was just an example to show you that the nested subclass inherits EVERYTHING from the main class. This also includes the nested subclass itself.
Consider basic inheritance:
Class B inherits the constant, inner class and public field from class A.
Now consider the case where B is a nested class of class A:
Now not only does class B inherit the constant, inner class InnerA and the public field, but it also inherits itself! Therefore, it would be possible to call class B through class B itself:
Altough in practice you would never use the second or third possibilities, it is still valid code.
Joined: Oct 31, 2006
Thanks Rob !!
That was really nice & simple example.
But wont it be recursive thing? Why would someone want to do such thing? Any peculiar reason or condition where one wants to implement such code?
Joined: Jan 30, 2000
There is no reason to do this, ever. The fact that it's possible at all is just because of an accidental overlap between two rules:
1. a nested class is a members of its enclosing class
2. a subclass inherits members from its superclasses
If the enclosing class is the superclass, then the class inherits itself as a member. Which is useless, but also harmless. Sun could have put in a special rule preventing this from being possible, but then people would ask "why isn't this possible?". It's kind of a waste of time either way.
The code above isn't recursive - there's no possibility of an infinite loop here; it's entirely the programmer's decision whether to type A.B or A.B.B or A.B.B.B, etc. The process stops automatically as soon as the programmer decides to stop wasting time typing in meaningless code. [ October 31, 2007: Message edited by: Jim Yingst ]