Question about static variable initializers
Hi, I'm going through JLS Third edition, on page 204, where the following example is shown:
package experiments;
/**
*
* @author mtedone
*/
publicclass Z{
staticint peek(){return j;}
staticint i = peek();
staticint j = 1;
/** Creates a new instance of Z */
public Z(){
}
}
package experiments;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Iterator;
/**
*
* @author mtedone
*/
publicclass Main{
/** Creates a new instance of Main */
public Main(){
}
/**
* @param args the command line arguments
*/
publicstaticvoid main(String[] args){
System.out.println(Z.i);
}
}
Which produces the output zero. JLS explains that the variable initializer for i uses the class method peek() to access the value for j before j is actually initialized with the value of 1. I understood that. However I thought that by changing the order, like the following code:
package experiments;
/**
*
* @author mtedone
*/
publicclass Z{
staticint j = 1;
staticint peek(){return j;}
staticint i = peek();
/** Creates a new instance of Z */
public Z(){
}
}
j would be first initialized to 1, but it still prints zero. Shall I assume, although it appears that JLS doesn't explicitly state so, that class methods in a class variable initializer expression, when these would access other class variables, willalways be executed before the actual class variable they refer to are initialized?

