It seems intuitively clear that in Java, instance variable intitializers are executed in the order in which they appear in the class declaration.
This certainly appears to be the case in the JDK I am using. For example, the following:
public class Clazz {
int x = 42;
int y = this.z;
int z = this.x;
void print() {
System.out.printf("%d %d %d\n", x, y, z);
}
public static void main(String[] args) {
new Clazz().print();
}
}
prints 42 0 42 (in other words, y picks up the default value of z).
Is this ordering actually guaranteed? I've been looking through the JLS, and can't find any explicit confirmation.
yis assigned,zis still not initialized, therefore it shows0.