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.
Exception checking for a variable initializer in a field declaration is specified in § 11.2.3 .
Variable initializers are also used in local variable declaration statements (§ 14.4 ) , where the
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.
If the keyword this 15.8.3 ) or the keyword super 15.11.2 , § 15.12 ) occurs in an initializ-
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
4.12.5 ) , even by devious programs (§ 13.4.9 ) .
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;
}
Search WWH ::




Custom Search