public class TestA {
static int x;
static TestA a;
static TestB b;
TestA() { x++;}
static void runTest() {
a = new TestA();
b = new TestB();
}
public static void main(String[] args) {
runTest();
runTest();
System.out.println(a.x);
System.out.println(b.x);
System.out.println(a.x + b.x);
}
}
class TestB extends TestA {
static int x;
TestB(){ x += 2;}
}
The result of this code is : 4 4 8 May I ask why? I tried to understand it by drawing a graph. But it never makes sense. How can x be changed to 4?