0

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.

Mel Torment
  • 49
  • 1
  • 7
  • 3
    There is no inheritance involved: it is the same field – Mark Rotteveel Jul 15 '18 at 09:35
  • 2
    You would know what happens if you used uour debugger to step through the code, line by line, or if you simply added println() logs to the code. – JB Nizet Jul 15 '18 at 09:37
  • The output should be A: 11 A: 11 A: 2 B: 11 B: 101 B: 5. There are two topics you need understsand: 1. order of initialization and instantiation. 2. field hiding. – Jacob Jul 15 '18 at 11:10

1 Answers1

0

if the value of the static variable z is "shared" among the subclass(es)?

Yes. static variables are class level variables, meaning they do not belong to an instance of a class but to the class itself. They are accessible from a child class as long as they are not declared private, and they are not inherited. There's only one static variable for all instances of the class regardless if it's an instance of the class itself or an instance of any of its sub-classes. Thus:

A.z = 5; // z is 5 now
B.z = 7; // z is 7 now 
System.out.prinln(A.z); // will print 7 since there's only one z shared by everybody
                        // remember, z belongs to A not to an instance of A.

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?

Not quite. Before a child class is constructed, its parent class needs to be constructed. How can you extend something that doesn't exist =]? Thus a call to the parent constructor is made before any other instruction in the child constructor.

public B() {
    x++;
    z++
}

is equivalent to

public B() {
    super(); // initialize A before initializing B
    x++;
    z++
}

If you don't call super() explicitly, which unless you need to pass something to the parent constructor you don't have to, the compiler will insert super() for you. So in the B constructor, the constructor for A is called but it doesn't pass z to B it just does what you instructed it to do and moves on.

StaticBeagle
  • 5,070
  • 2
  • 23
  • 34