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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CLASSES Constructor Body 8.8.7<br />

• E is declared in the throws clause of the constructor that is invoked.<br />

If an anonymous class instance creation expression appears within an explicit<br />

constructor invocation statement, then the anonymous class may not refer to any<br />

of the enclosing instances of the class whose constructor is being invoked.<br />

For example:<br />

class Top {<br />

int x;<br />

class Dummy {<br />

Dummy(Object o) {}<br />

}<br />

class Inside extends Dummy {<br />

Inside() {<br />

super(new Object() { int r = x; }); // error<br />

}<br />

Inside(final int y) {<br />

super(new Object() { int r = y; }); // correct<br />

}<br />

}<br />

}<br />

Let C be the class being instantiated, let S be the direct superclass of C, and let i be<br />

the instance being created. <strong>The</strong> evaluation of an explicit constructor invocation<br />

proceeds as follows:<br />

• First, if the constructor invocation statement is a superclass constructor invocation,<br />

then the immediately enclosing instance of i with respect to S (if any)<br />

must be determined. Whether or not i has an immediately enclosing instance<br />

with respect to S is determined by the superclass constructor invocation as follows:<br />

DRAFT<br />

◆ If S is not an inner class, or if the declaration of S occurs in a static context,<br />

no immediately enclosing instance of i with respect to S exists. A compiletime<br />

error occurs if the superclass constructor invocation is a qualified<br />

superclass constructor invocation.<br />

◆ Otherwise:<br />

❖ If the superclass constructor invocation is qualified, then the Primary<br />

expression p immediately preceding ".super" is evaluated. If the primary<br />

expression evaluates to null, aNullPointerException is raised, and<br />

the superclass constructor invocation completes abruptly. Otherwise, the<br />

result of this evaluation is the immediately enclosing instance of i with<br />

245

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

Saved successfully!

Ooh no, something went wrong!