Java Reference
In-Depth Information
Point p = new Point();
System.out.println(p.x + ", " + p.y);
}
}
This program produces the output:
1, 5
because the assignments to
x
and
y
occur whenever a new
Point
is created.
initializer is evaluated and the assignment performed each time the local variable declara-
tion statement is executed.
8.3.2.1. Initializers for Class Variables
If a reference by simple name to any instance variable occurs in an initialization expression
for a class variable, then a compile-time error occurs.
ation expression for a class variable, then a compile-time error occurs.
At run time,
static
fields that are
final
and that are initialized with constant expressions
(§
15.28
)
are initialized first (§
12.4.2
)
. This also applies to such fields in interfaces (§
9.3.1
)
.
These fields are “constants” that will never be observed to have their default initial values
Use of class variables whose declarations appear textually after the use is sometimes
restricted, even though these class variables are in scope. See §
8.3.2.3
for the precise
rules governing forward reference to class variables.
8.3.2.2. Initializers for Instance Variables
Initialization expressions for instance variables may use the simple name of any static vari-
able declared in or inherited by the class, even one whose declaration occurs textually later.
Example 8.3.2.2-1. Out-of-order Field Initialization
class Test {
float f = j;
static int j = 1;
}