Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope (§6.3). Specifically, it is a compile-time error if all of the following are true:
- The declaration of a class variable in a class or interface C appears textually after a use of the class variable;
- The use is a simple name in either a class variable initializer of C or a static initializer of C;
- The use is not on the left hand side of an assignment;
- C is the innermost class or interface enclosing the use.
Use of instance variables whose declarations appear textually after the use is sometimes restricted, even though these instance variables are in scope. Specifically, it is a compile-time error if all of the following are true:
- The declaration of an instance variable in a class or interface C appears textually after a use of the instance variable;
- The use is a simple name in either an instance variable initializer of C or an instance initializer of C;
- The use is not on the left hand side of an assignment;
- C is the innermost class or interface enclosing the use.
Example 8.3.3-1. Restrictions on Field Initialization
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;
}