SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
When you do things right, people won't be sure you've done anything at all.
Janeice DelVecchio wrote:Why isn't p a member of the Child class, in which case, the output would be "Child."
The go() methods there aren't really overridden, because they have no visibility, right? You wouldn't be able to do this:
OCPJP (310-065) - 88%
When you do things right, people won't be sure you've done anything at all.
Janeice DelVecchio wrote:So it compiles. But will it run?
OCPJP (310-065) - 88%
When you do things right, people won't be sure you've done anything at all.
Frank Callahan wrote:Hi Malte,
Does it run from the command line, or in Eclipse or some other IDE? If on the command line, could you provide the details, including the java version and what you typed in?
Thanks!
Frank
OCPJP (310-065) - 88%
Frank Callahan wrote:Janeice,
Right, the real puzzle here is why the private instance method go() is visible through the Parent p reference declared in the Child main method. I understand that static methods can't be overridden so this might make sense if go was static (ignoring that it's private). Do all methods of static inner classes behave as if they are static? And why is the access modifier ignored? Very confusing.
OCPJP (310-065) - 88%
OCPJP (310-065) - 88%
Frank Callahan wrote:Maybe I have this one figured out... Here's some code that does not compile, that moves the Parent class outside PrivateTest:
The rule seems to be that any member of any nested class is visible to any other nested class within the same enclosing class.
OCPJP (310-065) - 88%
Frank Callahan wrote:So. static inner classes are not just the same as top level classes. They have full access to all the members of the enclosing class.
OCPJP (310-065) - 88%
Frank Callahan wrote:The K&B book's section on static nested classes describes them as follows:
"You’ll sometimes hear static nested classes referred to as static inner classes, but they really aren’t inner classes at all, by the standard definition of an inner class. While an inner class (regardless of the flavor) enjoys that special relationship with the outer class (or rather the instances of the two classes share a relationship), a static nested class does not. It is simply a non-inner (also called “top-level”) class scoped within another. " (p. 680)
To me, the visibility to static inner classes of other private nested classes declared inside the same outer class scope is a major distinction between regular "top-level" classes and static inner classes. Saying that they are not really inner classes seems wrong to me at this point, since they share the major advantage of non-static inner classes -- access to everything declared in the enclosing class regardless of access modifiers. Of course, the static inner class can't see instance variables directly, since it is static, but it can declare a reference to the outer class and access private members of the outer class that way, and can declare references to private classes declared inside the outer class and access their private members.
I don't know how useful static inner classes are in a practical sense, but they do seem to be full fledged inner classes.
OCPJP (310-065) - 88%
A class body may contain declarations of members of the class, that is, fields (§8.3), classes (§8.5), interfaces (§8.5) and methods (§8.4). A class body may also contain instance initializers (§8.6), static initializers (§8.7), and declarations of constructors (§8.8) for the class. ... The scope of a declaration of a member m declared in or inherited by a class type C is the entire body of C, including any nested type declarations.
...., otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
Consider Paul's rocket mass heater. |