Given the code below:
public class A {
private int i = 6;
private int j = i;
public A() {
i = 5;
}
public static void main(String[] args) {
A a = new A();
System.out.println(a.i + a.j);
}
}
Why is the output of this addition is 11 and not 10? From what I understand, the constructor runs last after static and instance variables are initialised. Therefore if the constructor is the last to be triggered it should be setting "both" i and j to 5 since they point to the same number. Could someone please explain step by step what is happening here so that once executed the value is 11. Any feedback would be greatly appreciated.