Java Reference
In-Depth Information
an expression.
Example 8.8.7-1. Constructor Bodies
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
}
class ColoredPoint extends Point {
static final int WHITE = 0, BLACK = 1;
int color;
ColoredPoint(int x, int y) {
this(x, y, WHITE);
}
ColoredPoint(int x, int y, int color) {
super(x, y);
this.color = color;
}
}
Here, the first constructor of
ColoredPoint
invokes the second, providing an additional
argument; the second constructor of
ColoredPoint
invokes the constructor of its super-
class
Point
, passing along the coordinates.
8.8.7.1. Explicit Constructor Invocations
ExplicitConstructorInvocation:
NonWildTypeArguments
opt
this (
ArgumentList
opt
) ;
NonWildTypeArguments
opt
super (
ArgumentList
opt
) ;
Primary
.
NonWildTypeArguments
opt
super (
ArgumentList
opt
) ;
NonWildTypeArguments:
<
ReferenceTypeList
>
ReferenceTypeList:
ReferenceType
ReferenceTypeList
,
ReferenceType
Explicit constructor invocation statements can be divided into two kinds:
•
Alternate constructor invocations
begin with the keyword
this
(possibly prefaced
with explicit type arguments). They are used to invoke an alternate constructor of
the same class.