In the code "a" is set after is defined and not before.However, a static block can only set the value of a variable that is defined afterwards.
Code: Select all
public class Temp{
static int a;
static{
System.out.println(a); //valid, a is defined before the static block
//System.out.println(b); //INVALID, b is defined after the static block
b = 10; //valid because b is being assigned a value
a = 111; // value set after it has been defined and not before ???
m(); //valid even though m is defined later
}
static void m(){
System.out.println(b); //valid, a method can do anything with a variable that is declared later in the code
}
static int b;
public static void main(String[] args){
}
//another static block
static{
System.out.println(b);
}
}