Reputation: 563
My question comes from the example at the end of Chapter 6.7 of the Java Language Specification :
The difference between a fully qualified name and a canonical name can be seen in code such as:
package p;
class O1 { class I {} }
class O2 extends O1 {}
Both
p.O1.I
andp.O2.I
are fully qualified names that denote the member class I, but onlyp.O1.I
is its canonical name.
I don't quite understand why p.O2.I
is not a canonical name.
According to 6.7:
Every primitive type, named package, top level class, and top level interface has a canonical name:
• For every primitive type, named package, top level class, and top level interface, the canonical name is the same as the fully qualified name.
Each member class, member interface, and array type may have a canonical name:
• A member class or member interface M declared in another class or interface C has a canonical name if and only if C has a canonical name.
My understanding is that p.O2.I
is "a member class or interface M declared in another class C", Class O2
is a top-level class, so it has a canonical name p.O2
. Then according to the rule,Why isn't p.O2.I
a canonical name?
In addition, I also can't see the difference between a canonical name and a fully qualified name (compared to the previous quoted paragraph):
Each member class, member interface, and array type may have a fully qualified name:
• A member class or member interface M of another class or interface C has a fully qualified name if and only if C has a fully qualified name.
Can anyone explain this to me in detail? Thank you for your reading.
Upvotes: -1
Views: 83
Reputation: 273540
The keyword here is "declared in". Compare:
A member class or member interface M of another class or interface C has a fully qualified name if and only if C has a fully qualified name.
In that case, the fully qualified name of M consists of the fully qualified name of C, followed by ".", followed by the simple name of M.
with
A member class or member interface M declared in another class or interface C has a canonical name if and only if C has a canonical name.
In that case, the canonical name of M consists of the canonical name of C, followed by ".", followed by the simple name of M.
I
is a member class of O1
, and it is also a member class of O2
because of inheritance.
But I
is only declared in O1
. It is not declared in O2
. More specifically, the declaration of I
is in the body of O1
, not O2
.
Therefore, p.O2.I
is not a canonical name of I
.
Upvotes: 6