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.

CLASSES Static Initializers 8.7<br />

agate information about what exceptions might be raised by an anonymous class’<br />

instance initializer to the surrounding expression. Named classes, on the other<br />

hand, can be instantiated in many places. <strong>The</strong>refore the only way to propagate<br />

information about what exceptions might be raised by an instance initializer of a<br />

named class is through the throws clauses of its constructors. It follows that a<br />

more liberal rule can be used in the case of anonymous classes. Similar comments<br />

apply to instance variable initializers.<br />

It is a compile-time error if an instance initializer cannot complete normally<br />

(§14.21). If a return statement (§14.17) appears anywhere within an instance initializer,<br />

then a compile-time error occurs.<br />

Use of instance variables whose declarations appear textually after the use is<br />

sometimes restricted, even though these instance variables are in scope. See<br />

§8.3.2.3 for the precise rules governing forward reference to instance variables.<br />

Instance initializers are permitted to refer to the current object this (§15.8.3),<br />

to any type variables (§4.4) in scope and to use the keyword super (§15.11.2,<br />

§15.12).<br />

8.7 Static Initializers<br />

Any static initializers declared in a class are executed when the class is initialized<br />

and, together with any field initializers (§8.3.2) for class variables, may be used to<br />

initialize the class variables of the class (§12.4).<br />

StaticInitializer:<br />

static Block<br />

It is a compile-time error for a static initializer to be able to complete abruptly<br />

(§14.1, §15.6) with a checked exception (§11.2). It is a compile-time error if a<br />

static initializer cannot complete normally (§14.21).<br />

<strong>The</strong> static initializers and class variable initializers are executed in textual<br />

order.<br />

Use of class variables whose declarations appear textually after the use is<br />

sometimes restricted, even though these class variables are in scope. See §8.3.2.3<br />

for the precise rules governing forward reference to class variables.<br />

If a return statement (§14.17) appears anywhere within a static initializer,<br />

then a compile-time error occurs.<br />

If the keyword this (§15.8.3) or any type variable (§4.4) defined outside the<br />

initializer or the keyword super (§15.11, §15.12) appears anywhere within a<br />

static initializer, then a compile-time error occurs.<br />

DRAFT<br />

239

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

Saved successfully!

Ooh no, something went wrong!