Below is the code,
class Z{
static int peekj(){
return j;
}
static int peekk(){
return k;
}
static int i = peekj();
static int h = peekk();
static final int j = 1;
static int k = 1;
}
public class ClassAndInterfaceInitialization {
public static void main(String[] args) {
System.out.println(Z.i);
System.out.println(Z.h);
}
}
After following the forward reference rule for static initialization, I see the output as:
1
0
After class Z is loaded & linked, In the initialization phase, variable j being final is very firstly initialized with 1. variable k is also initialized with 1.
But the output gives 0 for variable k.
How do I understand this?
Note: Compiler actually replaces value of variable j wherever it is referenced following forward reference rules, unlike k
System.out.println(Z.peekk());outputs 1 where asSystem.out.println(Z.h);outputs 0. How?