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.

EXPRESSIONS Anonymous Class Declarations 15.9.5<br />

<strong>The</strong> value of a class instance creation expression is a reference to the newly<br />

created object of the specified class. Every time the expression is evaluated, a<br />

fresh object is created.<br />

15.9.5 Anonymous Class Declarations<br />

An anonymous class declaration is automatically derived from a class instance<br />

creation expression by the compiler.<br />

An anonymous class is never abstract (§8.1.1.1). An anonymous class is<br />

always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.2). An anonymous<br />

class is always implicitly final (§8.1.1.2).<br />

15.9.5.1 Anonymous Constructors<br />

An anonymous class cannot have an explicitly declared constructor. Instead, the<br />

compiler must automatically provide an anonymous constructor for the anonymous<br />

class. <strong>The</strong> form of the anonymous constructor of an anonymous class C with<br />

direct superclass S is as follows:<br />

• If S is not an inner class, or if S is a local class that occurs in a static context,<br />

then the anonymous constructor has one formal parameter for each actual<br />

argument to the class instance creation expression in which C is declared. <strong>The</strong><br />

actual arguments to the class instance creation expression are used to determine<br />

a constructor cs of S, using the same rules as for method invocations<br />

(§15.12). <strong>The</strong> type of each formal parameter of the anonymous constructor<br />

must be identical to the corresponding formal parameter of cs.<br />

<strong>The</strong> body of the constructor consists of an explicit constructor invocation<br />

(§8.8.7.1) of the form super(...), where the actual arguments are the formal<br />

parameters of the constructor, in the order they were declared.<br />

• Otherwise, the first formal parameter of the constructor of C represents the<br />

value of the immediately enclosing instance of i with respect to S. <strong>The</strong> type of<br />

this parameter is the class type that immediately encloses the declaration of S.<br />

<strong>The</strong> constructor has an additional formal parameter for each actual argument<br />

to the class instance creation expression that declared the anonymous class.<br />

<strong>The</strong> nth formal parameter e corresponds to the n –<br />

1 st actual argument. <strong>The</strong><br />

actual arguments to the class instance creation expression are used to determine<br />

a constructor cs of S, using the same rules as for method invocations<br />

(§15.12). <strong>The</strong> type of each formal parameter of the anonymous constructor<br />

must be identical to the corresponding formal parameter of cs. <strong>The</strong> body of<br />

the constructor consists of an explicit constructor invocation (§8.8.7.1) of the<br />

form o.super(...), where o is the first formal parameter of the constructor, and<br />

DRAFT<br />

429

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

Saved successfully!

Ooh no, something went wrong!