I have this piece of code and my question is, if the value of the static variable z is "shared" among the subclass(es)? Concrete: When I declare b
does this mean that first the constructor of A is used and passes the modified values of z to the constructor of B?
public class Test {
public static void main(String[] args){
A a = new A();
System.out.println("A: " + a.x);
a.print();
A b = new B();
System.out.println("B: " + b.x);
a.print();
}
}
public class A {
public static int z; // default: 0
public int x = 10;
public A() {
x++;
z++; // z = 1
}
print(){
System.out.println("A: "+ x);
System.out.println("A: "+ ++z); // z = 2
}
}
public class B extends A {
public int x = 100;
public B() {
x++;
z++; //z is not 3. z = 4 (because of the constructor of A that
uses the modified values of z?)
}
public void print() {
System.out.println("B: "+ x);
System.out.println("B: "+ ++z); // z = 5
}
}
The output is :
A: 11 A: 11 A: 2 B: 11 B: 102 B: 5
Are these values passed to the subclass because z is static and that means if I change its values they will be changed while running my code if i don't change z by passing it another concrete value in A?
I am confused. Hopefully somebody can explain this to me.