15.10.1
Field Access Using a Primary
EXPRESSIONS
15.10.1 Field Access Using a Primary
The type of the
Primary
must be a reference type
T
, or a compile time error
occurs. The meaning of the field access expression is determined as follows:
If the identifier names several accessible member fields of type
T
, then the
field access is ambiguous and a compile time error occurs.
If the identifier does not name an accessible member field of type
T
, then the
field access is undefined and a compile time error occurs.
Otherwise, the identifier names a single accessible member field of type
T
and
the type of the field access expression is the declared type of the field. At run
time, the result of the field access expression is computed as follows:
N
If the field is
static
:
O
If the field is
final
, then the result is the value of the specified class vari
able in the class or interface that is the type of the
Primary
expression.
O
If the field is not
final
, then the result is a variable, namely, the specified
class variable in the class that is the type of the
Primary
expression.
N
If the field is not
static
:
O
If the value of the
Primary
is
null
, then a
NullPointerException
is
thrown.
O
If the field is
final
, then the result is the value of the specified instance
variable in the object referenced by the value of the
Primary
.
O
If the field is not
final
, then the result is a variable, namely, the specified
instance variable in the object referenced by the value of the
Primary
.
Note, specifically, that only the type of the
Primary
expression, not the class of the
actual object referred to at run time, is used in determining which field to use.
Thus, the example:
class S { int x = 0; }
class T extends S { int x = 1; }
class Test {
public static void main(String[] args) {
T t = new T();
System.out.println("t.x=" + t.x + when("t", t));
S s = new S();
System.out.println("s.x=" + s.x + when("s", s));
320
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved