-1
public class A { 
    String bar = "A.bar";

    A() { foo(); }

   public void foo() {
    System.out.println("A.foo(): bar = " + bar);
   }
 }

public class B extends A {
    String bar = "B.bar";

    B(){ foo(); }

    public void foo(){
        System.out.println("B.foo(): bar = " + bar);
    }
}

public class C {
    public static void main(String[] args){
        A a = new B();
        System.out.println("a.bar = " + a.bar);
        a.foo();
    }
}

Why does the first output has bar = null? Is it because B.foo() is being called before class B is created? if yes then how come B.foo() can be called? Or is it because the field bar in B.foo() is trying to get bar field from A but cannot access it?

My question is different from the one linked, i'm not asking about the call order ,i'm asking why does the first output is null? the other question is not about fields or null variables.

I don't understand how the bar variable in B.foo is null if it is defined in A and in B.

Raz
  • 160
  • 1
  • 4
  • 14
  • 1
    Possible duplicate of [In what order do static/instance initializer blocks in Java run?](https://stackoverflow.com/questions/2007666/in-what-order-do-static-instance-initializer-blocks-in-java-run) – Tom Jul 19 '17 at 08:04
  • Please paste code as *text* rather than images. – Jon Skeet Jul 19 '17 at 08:04

3 Answers3

0

At first I would tell you that the variable bar in the class A is compeletely different from the other variable bar in the class B.

may be you would like that they are one variable like this:

public class B extends A{

public B (){
        bar = "B.bar";´
    foo();
}
void foo(){
    System.out.println("B.foo bar ="+ bar);
}

}

and you will have the result:

B.foo bar =A.bar
B.foo bar =B.bar
a.bar=B.bar
B.foo bar =B.bar

when not, tell be please, and I will also describe your Special case for you (Why is bar = null)

Hasan
  • 296
  • 1
  • 8
  • 23
0

The reason the first print is B.foo(): bar=null is because when you invoke the line A a = new B(); what is happening is that you create an object of type B, which calls the constructor of the object of type B. But, that constructor has a "hidden" super call for the constructor of A. The problem is that the class B haven't yet been create (you are now in the process of creating the class A first), therefore their arguments (such as bar) are not initialized, meaning they are assigned the null value. You still get the B.foo(): since the call to the dynamic function is by the dynamic dispatch, thus calling the function foo of B. Only after calling the constructor of A you call the constructor of B which then initializes the variables of B - including bar.

Mickey
  • 1,405
  • 2
  • 13
  • 33
  • well I would agree if foo was static, but it isn't so I don't understand how it can call to foo if there's no instance of B. – Raz Jul 19 '17 at 13:18
  • @Raz Because you don't initialize methods. The method belongs to the class `B`. The dynamic dispatch mechanisms tries to call to the `foo` of `A`, but when it "sees" that the dynamic type of the object is of type `B`, it simply calls the `foo` method of `B`. – Mickey Jul 19 '17 at 13:21
  • I tried to look if you can call a method of a class before the class has been initialized. and i found this question https://stackoverflow.com/questions/2394205/can-i-use-methods-of-a-class-without-instantiating-this-class which states that you can't – Raz Jul 19 '17 at 19:42
  • @Raz Probably because there is some middle ground - the variables haven't yet been defined, because the stage of creating the actual constructor of `B` hasn't begun, but the methods were already available because the dynamic dispatch is bound to call the method in `B` – Mickey Jul 19 '17 at 19:52
  • I found the answer, apparently the complier moves all field initialization right after calling super(). so when the constructor of B is called, first super() is called and after that bar gets initialized https://stackoverflow.com/questions/18830103/why-instance-variables-get-initialized-before-constructor-called – Raz Jul 20 '17 at 07:55
0

Hello this is the run of your Code line by line enter image description here

Hasan
  • 296
  • 1
  • 8
  • 23