I tried to understand the behavior of initialization order when static fields are initialized with a reference to the same enclosing class object.
public class Test {
static final Test t=new Test();
static int a=5;
Test(){
System.out.println("a="+a);
}
public static void main(String[] args) {
new Test();
}
}
Output of above piece of code is:
a=0
a=5
If I modify variable a
to anything else other than plain static
:
static final a=5;
a=5;
final a=5;
The output is:
a=5
a=5
Why is this behavior?
Note that the output is a=5 & a=5
even when both t & a
are declared as static final
in which case t
precedes the declaration of a