References to a field are sometimes restricted, even through the field is in scope. The following rules constrain forward references to a field (where the use textually precedes the field declaration) as well as self-reference (where the field is used in its own initializer).
For a reference by simple name to a class variable f declared in class or interface C, it is a compile-time error if:
- The reference appears either in a class variable initializer of C or in a static initializer of C (§8.7); and
- The reference appears either in the initializer of
f's own declarator or at a point to the left off's declarator; and - The reference is not on the left hand side of an assignment expression (§15.26); and
- The innermost class or interface enclosing the reference is C.
For a reference by simple name to an instance variable f declared in class C, it is a compile-time error if:
- The reference appears either in an instance variable initializer of C or in an instance initializer of C (§8.6); and
- The reference appears in the initializer of
f's own declarator or at a point to the left off's declarator; and - The reference is not on the left hand side of an assignment expression (§15.26); and
- The innermost class enclosing the reference is C.
Example 8.3.3-1. Restrictions on Field References
A compile-time error occurs for this program:
class Test1 {
int i = j; // compile-time error:
// incorrect forward reference
int j = 1;
}
whereas the following program compiles without error:
class Test2 {
Test2() { k = 2; }
int j = 1;
int i = j;
int k;
}
even though the constructor for Test2 (§8.8) refers to the field k that is declared three lines later.
The restrictions above are designed to catch, at compile time, circular or otherwise malformed initializations. Thus, both:
class Z {
static int i = j + 2;
static int j = 4;
}
and:
class Z {
static { i = j + 2; }
static int i, j;
static { j = 4; }
}
result in compile-time errors. Accesses by methods are not checked in this way, so:
class Z {
static int peek() { return j; }
static int i = peek();
static int j = 1;
}
class Test {
public static void main(String[] args) {
System.out.println(Z.i);
}
}
produces the output:
0
because the variable initializer for i uses the class method peek to access the value of the variable j before j has been initialized by its variable initializer, at which point it still has its default value (§4.12.5).
A more elaborate example is:
class UseBeforeDeclaration {
static {
x = 100;
// ok - assignment
int y = x + 1;
// error - read before declaration
int v = x = 3;
// ok - x at left hand side of assignment
int z = UseBeforeDeclaration.x * 2;
// ok - not accessed via simple name
Object o = new Object() {
void foo() { x++; }
// ok - occurs in a different class
{ x++; }
// ok - occurs in a different class
};
}
{
j = 200;
// ok - assignment
j = j + 1;
// error - right hand side reads before declaration
int k = j = j + 1;
// error - illegal forward reference to j
int n = j = 300;
// ok - j at left hand side of assignment
int h = j++;
// error - read before declaration
int l = this.j * 3;
// ok - not accessed via simple name
Object o = new Object() {
void foo(){ j++; }
// ok - occurs in a different class
{ j = j + 1; }
// ok - occurs in a different class
};
}
int w = x = 3;
// ok - x at left hand side of assignment
int p = x;
// ok - instance initializers may access static fields
static int u =
(new Object() { int bar() { return x; } }).bar();
// ok - occurs in a different class
static int x;
int m = j = 4;
// ok - j at left hand side of assignment
int o =
(new Object() { int bar() { return j; } }).bar();
// ok - occurs in a different class
int j;
}