---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 13 Edition"
java_version: 13
chapter: "Chapter 6. Names"
section: "6.5.2"
heading: "6.5.2. Reclassification of Contextually Ambiguous Names"
parent: "6.5. Determining the Meaning of a Name"
source: https://docs.oracle.com/javase/specs/jls/se13/html/jls-6.html#jls-6.5.2
id: "6.5.2"
url: https://jvm.obfuscated.software/jls/se13/6.5.2.html
raw: https://jvm.obfuscated.software/jls/se13/6.5.2.md
---

# 6.5.2. Reclassification of Contextually Ambiguous Names

An *AmbiguousName* is then reclassified as follows.

If the *AmbiguousName* is a simple name, consisting of a single *Identifier*:

- If the *Identifier* appears within the scope ([§6.3](6.3.md)) of a local variable declaration ([§14.4](14.4.md)) or parameter declaration ([§8.4.1](8.4.1.md), [§8.8.1](8.8.1.md), [§14.20](14.20.md)) or field declaration ([§8.3](8.3.md)) with that name, then the *AmbiguousName* is reclassified as an *ExpressionName*.
- Otherwise, if a field of that name is declared in the compilation unit ([§7.3](7.3.md)) containing the *Identifier* by a single-static-import declaration ([§7.5.3](7.5.3.md)), or by a static-import-on-demand declaration ([§7.5.4](7.5.4.md)) then the *AmbiguousName* is reclassified as an *ExpressionName*.
- Otherwise, if the *Identifier* is a valid *TypeIdentifier* and appears within the scope ([§6.3](6.3.md)) of a top level class ([§8 (*Classes*)](8.md)) or interface type declaration ([§9 (*Interfaces*)](9.md)), a local class declaration ([§14.3](14.3.md)) or member type declaration ([§8.5](8.5.md), [§9.5](9.5.md)) with that name, then the *AmbiguousName* is reclassified as a *TypeName*.
- Otherwise, if the *Identifier* is a valid *TypeIdentifier* and a type of that name is declared in the compilation unit ([§7.3](7.3.md)) containing the *Identifier*, either by a single-type-import declaration ([§7.5.1](7.5.1.md)), or by a type-import-on-demand declaration ([§7.5.2](7.5.2.md)), or by a single-static-import declaration ([§7.5.3](7.5.3.md)), or by a static-import-on-demand declaration ([§7.5.4](7.5.4.md)), 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 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` as a type name. It is usually redundant, because the rules for declarations already prevent the introduction of types named `var`. However, in some cases, a compiler may find a binary class named `var`, 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 `org` is 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 `rpgpoet` in any compilation unit of package `org` (and we know that there is no such class or interface because package `org` has a subpackage named `rpgpoet`), the qualified name `org.rpgpoet` is reclassified as a *PackageName*.
- Next, because package `org.rpgpoet` has an accessible ([§6.6](6.6.md)) interface type named `Music`, the qualified name `org.rpgpoet.Music` is reclassified as a *TypeName*.
- Finally, because the name `org.rpgpoet.Music` is a *TypeName*, the qualified name `org.rpgpoet.Music.wizards` is reclassified as an *ExpressionName*.
