An AmbiguousName is then reclassified as follows.
If the AmbiguousName is a simple name, consisting of a single Identifier, then:
- If the Identifier appears within the scope of a declaration (§6.3) denoting either a local variable, formal parameter, exception parameter, or field with that name (§14.4, §8.4.1, §8.8.1, §15.27.1, §14.20, §8.3), then the AmbiguousName is reclassified as an ExpressionName.
- Otherwise, if the Identifier is a valid TypeIdentifier (§3.8) and appears within the scope of a declaration denoting a class, interface, or type parameter with that name (§8.1, §9.1, §8.4.4, §8.8.4), then the AmbiguousName is reclassified as a TypeName.
- Otherwise, the AmbiguousName is reclassified as a PackageName. A later step determines whether or not a package of that name actually exists.
If the AmbiguousName is a qualified name, consisting of a name, a ".", and an Identifier, then the name to the left of the "." is first reclassified, for it is itself an AmbiguousName. There is then a choice:
-
If the name to the left of the "
." is reclassified as a PackageName, then:- If the Identifier is a valid TypeIdentifier, and there is a package whose name is the name to the left of the "
.", and that package contains a declaration of a type whose name is the same as the Identifier, then this AmbiguousName is reclassified as a TypeName. - Otherwise, this AmbiguousName is reclassified as a PackageName. A later step determines whether or not a package of that name actually exists.
- If the Identifier is a valid TypeIdentifier, and there is a package whose name is the name to the left of the "
-
If the name to the left of the "
." is reclassified as a TypeName, then:- If the Identifier is the name of a method or field of the type denoted by TypeName, then this AmbiguousName is reclassified as an ExpressionName.
- Otherwise, if the Identifier is a valid TypeIdentifier and is the name of a member type of the type denoted by TypeName, then this AmbiguousName is reclassified as a TypeName.
- Otherwise, a compile-time error occurs.
-
If the name to the left of the "
." is reclassified as an ExpressionName, then this AmbiguousName is reclassified as an ExpressionName. A later step determines whether or not a member with the name Identifier actually exists.
The requirement that a potential type name be "a valid TypeIdentifier" prevents treating var and yield as a type name. It is usually redundant, because the rules for declarations already prevent the introduction of types named var and yield. However, in some cases, a compiler may find a binary class named var or yield, and we want to be clear that such classes can never be named. The simplest solution is to consistently check for a valid TypeIdentifier.
Example 6.5.2-1. Reclassification of Contextually Ambiguous Names
Consider the following contrived "library code":
package org.rpgpoet;
import java.util.Random;
public interface Music { Random[] wizards = new Random[4]; }
and then consider this example code in another package:
package bazola;
class Gabriel {
static int n = org.rpgpoet.Music.wizards.length;
}
First of all, the name org.rpgpoet.Music.wizards.length is classified as an ExpressionName because it functions as a PostfixExpression. Therefore, each of the names:
org.rpgpoet.Music.wizards
org.rpgpoet.Music
org.rpgpoet
org
is initially classified as an AmbiguousName. These are then reclassified:
- The simple name
orgis reclassified as a PackageName (since there is no variable or type named org in scope). - Next, assuming that there is no class or interface named
rpgpoetin any compilation unit of packageorg(and we know that there is no such class or interface because packageorghas a subpackage namedrpgpoet), the qualified nameorg.rpgpoetis reclassified as a PackageName. - Next, because package
org.rpgpoethas an accessible (§6.6) interface type namedMusic, the qualified nameorg.rpgpoet.Musicis reclassified as a TypeName. - Finally, because the name
org.rpgpoet.Musicis a TypeName, the qualified nameorg.rpgpoet.Music.wizardsis reclassified as an ExpressionName.