Java Reference
In-Depth Information
Even if interface
PrintColors
were to give the value
3
to
YELLOW
rather than the value
8
, a reference to field
YELLOW
within interface
LotsOfColors
would still be considered
ambiguous.
Example 9.3-2. Multiply Inherited Fields
If a single field is inherited multiple times from the same interface because, for ex-
ample, both this interface and one of this interface's direct superinterfaces extend the
interface that declares the field, then only a single member results. This situation does
not in itself cause a compile-time error.
In the previous example, the fields
RED
,
GREEN
, and
BLUE
are inherited by interface
LotsOfColors
in more than one way, through interface
RainbowColors
and also through
interface
PrintColors
, but the reference to field
RED
in interface
LotsOfColors
is not con-
sidered ambiguous because only one actual declaration of the field
RED
is involved.
9.3.1. Initialization of Fields in Interfaces
Every field in the body of an interface must have an initialization expression, which need
It is a compile-time error if an initialization expression for an interface field contains a ref-
erence by simple name to the same field or to another field whose declaration occurs tex-
tually later in the same interface.
Example 9.3.1-1. Forward Reference to a Field
interface Test {
float f = j;
int j = 1;
int k = k + 1;
}
This program causes two compile-time errors, because
j
is referred to in the initializa-
tion of
f
before
j
is declared, and because the initialization of
k
refers to
k
itself.
ation expression for a field of an interface, then unless the occurrence is within the body of
The variable initializer is evaluated and the assignment performed exactly once, when the