Hi, I modified the code and added some SOPs to understand the flow of execution. I am giving the code here and also the output.
class A {
final static boolean i;
static {
System.out.println("in static A before callingB.f()");
B.f();
System.out.println("in static A before initialzation of A.i");
i = true;
System.out.println("in static A after initialzation of A.i, A.i= "+i);
}
}
class B {
final static boolean i;
static void f() {
}
static {
System.out.println("in static B before initialzation of B.i, A.i = " + A.i);
i = A.i;
System.out.println("in static B after initialzation of B.i, A.i = " + A.i + " B.i = " + i);
}
}
class C {
public static void main(
String[] args) {
System.out.println("A.i --> "+A.i);
System.out.println("B.i --> "+B.i);
}
}
The output is:
in static A before callingB.f()
in static B before initialzation of B.i, A.i = false
in static B after initialzation of B.i, A.i = false B.i = false
in static A before initialzation of A.i
in static A after initialzation of A.i, A.i= true
A.i --> true
B.i --> false
Still I cannot understand how can we use the value of A.i in class B before initialization.
And when I try to modify line 7 to
System.out.println("in static A before initialzation of A.i = " + i);
it gives me error (which is quite natural ofcourse).