Java Reference
In-Depth Information
• In qualified class instance creation expressions (§
15.9
), where an identifier occurs
immediately to the right of the leftmost
new
token to indicate a type that must be a
member of the compile-time type of the primary expression preceding the “
.
” pre-
ceding the leftmost
new
token.
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 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[0].length()
is not a name, but rather an
identifier appearing in a method invocation expression (§
15.12
). The occurrence of
expression, uses an identifier rather than a name to denote the member of interest.)
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. But field access, and method invocation qual-
ified by a
Primary
, and qualified class instance creation all denote 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, or class instance creation expression. Therefore, 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
noting a field.