Determining the Meaning of a Type Name

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:

  1. 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.
  2. 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.
  3. The scope of a top level type is all type declarations in the package in which the top level type is declared.
  4. 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.
  5. 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.

  1. 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.
  2. 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 1996

In this example the name java.util.Date must denote a type, so we first use the procedure recursively to determine if java.util is an accessible type or a package, which it is, and then look to see if the type Date is accessible in this package.

All of this lookup seems more difficult than I would want it to be, particularly because of inheritance.

Share

About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in Research. Bookmark the permalink.

Leave a Reply