Condensed rules for determining the meaning of a type name from the Java Language Specification, 3rd edition.
Scope of a Type
The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name (provided it is visible (§6.3.1)). A declaration is said to be in scope at a particular point in a program if and only if the declaration’s scope includes that point.
The scoping rules for various constructs are given in the sections that describe those constructs. For convenience, the rules are repeated here:
- The scope of a type imported by a single-type-import declaration (§7.5.1) or a type-import-on-demand declaration (§7.5.2) is all the class and interface type declarations (§7.6) in the compilation unit in which the import declaration appears.
- The scope of a member imported by a single-static-import declaration (§7.5.3) or a static-import-on-demand declaration (§7.5.4) is all the class and interface type declarations (§7.6) in the compilation unit in which the import declaration appears.
- The scope of a top level type is all type declarations in the package in which the top level type is declared.
- 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.
- The scope of the declaration of a member m declared in or inherited by an interface type I is the entire body of I, including any nested type declarations.
I wish I had an ordering for that.
Meaning of Type Names
The meaning of a name classified as a TypeName is determined as follows.
- Simple Type Names
If a type name consists of a single Identifier, then the identifier must occur in the scope of exactly one visible declaration of a type with this name, or a compile-time error occurs. The meaning of the type name is that type.- Qualified Type Names
If a type name is of the form Q.Id, then Q must be either a type name or a package name. If Id names exactly one type that is a member of the type or package denoted by Q, then the qualified type name denotes that type. If Id does not name a member type (§8.5, §9.5) within Q, or the member type named Id within Q is not accessible (§6.6), or Id names more than one member type within Q, then a compile-time error occurs.The example:
package wnj.test;
class Test {
public static void main(String[] args) {
java.util.Date date =
new java.util.Date(System.currentTimeMillis());
System.out.println(date.toLocaleString());
}
}
produced the following output the first time it was run:
Sun Jan 21 22:56:29 1996In this example the name
java.util.Date
must denote a type, so we first use the procedure recursively to determine ifjava.util
is an accessible type or a package, which it is, and then look to see if the typeDate
is accessible in this package.
All of this lookup seems more difficult than I would want it to be, particularly because of inheritance.