9.2. Determining the Meaning of a Name

The meaning of a name depends on the context in which it is used. The determination of the meaning of a name is done as follows.

  1. The context causes the possible meanings of a given name to be restricted to a subset of the following meanings:

    • A package name occurs in the context of a package declaration and of a type-import-on-demand declaration.

    • A type name occurs in several contexts, e.g., in import declarations, in extends- and implements- clauses, in the headers of field and method declarations, in queries, on the right hand side of rules, in property access expressions.

    • Expression names occur in several expression contexts, in queries and on the right hand side of rules.

    • Method names occur before the opening parenthesis of a method invocation expression, in queries and on the right hand side of rules.

    • Predicate names occur in queries.

  2. If the name is a simple name, consisting of a simple identifier, then it is determined for every possible meaning whether such a meaning of the identifier actually exists as described by the Java Language Specification (with the addition of predicate names which are treated analogously to type names, replacing type declarations by class predicate declarations). Now there are three cases:

    • If there exists no actual meaning at all, a compile-time error occurs.

    • If there exists exactly one actual meaning, then this is the meaning of the name.

    • Otherwise, obscuring (see the Java Language Specification) is used in order to disambiguate between the alternatives. Expression names obscure all other meanings of names. Package names are obscured by all other meanings of names. Predicate names obscure method names. If the context is a name predicate (Section 17.4.3, “Name Predicates”), type names obscure predicate names and method names. Otherwise predicate names and method names obscure type names.

  3. Otherwise, the name is a qualified name, consisting of a name q, a '.', and an identifier i. The meaning of the qualified name is determined recursively:

    • The meaning of q is determined by a recursion to step 2, where the set of possible meanings for q contains

      • package names iff the current set of possible meanings contains package names, type names, or predicate names,

      • type names iff the current set of possible meanings contains type names, predicate names, expression names, or method names.

      • expression names iff the current set of possible meanings contains expression names or method names.

      • The set never contains predicate names or method names.

    • Given the meaning of q, for every possible meaning of q.i it is determined whether such a meaning actually exists according to the Java Language Specification (again with the addition of predicate names). The disambiguation between different meanings is done as in step 2.