A name is used to refer to an entity declared in a program.
There are two forms of names: simple names and qualified names.
A simple name is a single identifier.
A qualified name consists of a name, a "." token, and an identifier.
In determining the meaning of a name (§6.5), the context in which the name appears is taken into account. The rules of §6.5 distinguish among contexts where a name must denote (refer to) a package (§6.5.3), a type (§6.5.5), a variable or value in an expression (§6.5.6), or a method (§6.5.7).
Packages and reference types have members which may be accessed by qualified names. As background for the discussion of qualified names and the determination of the meaning of names, see the descriptions of membership in §4.4, §4.5.2, §4.8, §4.9, §7.1, §8.2, §9.2, and §10.7.
Not all identifiers in a program are a part of a name. Identifiers are also used in the following situations:
-
In declarations (§6.1), where an identifier may occur to specify the name by which the declared entity will be known.
-
As labels in labeled statements (§14.7) and in
breakandcontinuestatements (§14.15, §14.16) that refer to statement labels.The identifiers used in labeled statements and their associated
breakandcontinuestatements are completely separate from those used in declarations. -
In field access expressions (§15.11), where an identifier occurs after a "
." token to indicate a member of the object denoted by the expression before the "." token, or the object denoted by thesuperor TypeName.superbefore the "." token. -
In some method invocation expressions (§15.12), wherever an identifier occurs after a "
." token and before a "(" token to indicate a method to be invoked for the object denoted by the expression before the "." token, or the type denoted by the TypeName before the "." token, or the object denoted by thesuperor TypeName.superbefore the "." token. -
In some method reference expressions (§15.13), wherever an identifier occurs after a "
::" token to indicate a method of the object denoted by the expression before the "::" token, or the type denoted by the TypeName before the "::" token, or the object denoted by thesuperor TypeName.superbefore the "::" token. -
In qualified class instance creation expressions (§15.9), where an identifier occurs to the right of the
newtoken to indicate a type that is a member of the compile-time type of the expression preceding thenewtoken. -
In element-value pairs of annotations (§9.7.1), to denote an element of the corresponding annotation type.
In this program:
class Test {
public static void main(String[] args) {
Class c = System.out.getClass();
System.out.println(c.toString().length() +
args[0].length() + args.length);
}
}
the identifiers Test, main, and the first occurrences of args and c are not names. Rather, they are identifiers used in declarations to specify the names of the declared entities. The names String, Class, System.out.getClass, System.out.println, c.toString, args, and args.length appear in the example.
The occurrence of length in args.length is a name because args.length is a qualified name (§6.5.6.2) and not a field access expression (§15.11). A field access expression, as well as a method invocation expression, a method reference expression, and a qualified class instance creation expression, uses an identifier rather than a name to denote the member of interest. Thus, the occurrence of length in args[0].length() is not a name, but rather an identifier appearing in a method invocation expression.
One might wonder why these kinds of expression use an identifier rather than a simple name, which is after all just an identifier. The reason is that a simple expression name is defined in terms of the lexical environment; that is, a simple expression name must be in the scope of a variable declaration (§6.5.6.1). On the other hand, field access, qualified method invocation, method references, and qualified class instance creation all refer to members whose names are not in the lexical environment. By definition, such names are bound only in the context provided by the Primary of the field access expression, method invocation expression, method reference expression, or class instance creation expression; or by the super of the field access expression, method invocation expression, or method reference expression; and so on. Thus, we denote such members with identifiers rather than simple names.
To complicate things further, a field access expression is not the only way to denote a field of an object. For parsing reasons, a qualified name is used to denote a field of an in-scope variable. (The variable itself is denoted with a simple name, alluded to above.) It is necessary for access control (§6.6) to apply to both denotations of a field.