Java Reference
In-Depth Information
15.9.5.1. Anonymous Constructors
An anonymous class cannot have an explicitly declared constructor. Instead, a Java com-
piler must automatically provide an anonymous constructor for the anonymous class. The
form of the anonymous constructor of an anonymous class
C
with direct superclass
S
is as
follows:
• If
S
is not an inner class, or if
S
is a local class that occurs in a static context, then
the anonymous constructor has one formal parameter for each actual argument to
the class instance creation expression in which
C
is declared.
The actual arguments to the class instance creation expression are used to determ-
The type of each formal parameter of the anonymous constructor must be identical
to the corresponding formal parameter of
cs
.
The body of the constructor consists of an explicit constructor invocation
ers of the constructor, in the order they were declared.
• Otherwise, the first formal parameter of the constructor of
C
represents the value
of the immediately enclosing instance of
i
with respect to
S
. The type of this para-
meter is the class type that immediately encloses the declaration of
S
.
The constructor has an additional formal parameter for each actual argument to the
class instance creation expression that declared the anonymous class. The
n
'th
formal parameter
e
corresponds to the
n-1
'th actual argument.
The actual arguments to the class instance creation expression are used to determ-
The type of each formal parameter of the anonymous constructor must be identical
to the corresponding formal parameter of
cs
.
The body of the constructor consists of an explicit constructor invocation
structor, and the actual arguments are the subsequent formal parameters of the con-
structor, in the order they were declared.