10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15.11.1 Field Access Using a Primary EXPRESSIONS<br />

436<br />

capture conversion (§5.1.10). At run time, the result of the field access expression<br />

is computed as follows:<br />

◆ If the field is static:<br />

❖ <strong>The</strong> Primary expression is evaluated, and the result is discarded. If evaluation<br />

of the Primary expression completes abruptly, the field access<br />

expression completes abruptly for the same reason.<br />

❖ If the field is final, then the result is the value of the specified class variable<br />

in the class or interface that is the type of the Primary expression.<br />

❖ If the field is not final, then the result is a variable, namely, the specified<br />

class variable in the class that is the type of the Primary expression.<br />

◆ If the field is not static:<br />

❖ <strong>The</strong> Primary expression is evaluated. If evaluation of the Primary expression<br />

completes abruptly, the field access expression completes abruptly<br />

for the same reason.<br />

❖ If the value of the Primary is null, then a NullPointerException is<br />

thrown.<br />

❖ If the field is final, then the result is the value of the specified instance<br />

variable in the object referenced by the value of the Primary.<br />

❖ If the field is not final, then the result is a variable, namely, the specified<br />

instance variable in the object referenced by the value of the Primary.<br />

Note, specifically, that only the type of the Primary expression, not the class of the<br />

actual object referred to at run time, is used in determining which field to use.<br />

Thus, the example:<br />

DRAFT<br />

class S { int x = 0; }<br />

class T extends S { int x = 1; }<br />

class Test {<br />

public static void main(String[] args) {<br />

T t = new T();<br />

System.out.println("t.x=" + t.x + when("t", t));<br />

S s = new S();<br />

System.out.println("s.x=" + s.x + when("s", s));<br />

s = t;<br />

System.out.println("s.x=" + s.x + when("s", s));<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!