Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java- why does it print null?

Tags:

java

runtime

public class Base {
public Base() {
    x = 0;
    bar();
}

public Base(int x) {
    this.x = x;
    foo();
}

public void foo() {
    System.out.println("Base.foo : " + x);
}

private void bar() {
    System.out.println("Base.bar:" + x.toString());
}

protected Integer x;
}

    public class Derived extends Base {
       public Derived() {
       bar();
       }
       public Derived(int x, int y) {
         super(x);
         this.y = y;
       }
       public void foo() {
         System.out.println("Derived.foo : " + x + ", " + y);
       }
       public void bar() {
         System.out.println("Derived.bar:" + x.toString() + ", " + y.toString());
       }
       private Integer y;


       public static void main(String[] args) {
        Base b = new Derived(10, 20);
      }
}

Why does it print "Derived.foo:" 10, nulll and not 20 instead of the null? y is a private variable of Derived, and it was initialized with 20. it's in its scope.. so why is it null?

like image 459
Numerator Avatar asked Sep 06 '11 13:09

Numerator


People also ask

Why null is printing in Java?

It's just the string and the character array parameters that cause ambiguity as character arrays and objects can happily coexist. The char array null cannot be printed by the PrintStream since it causes a NullPointerException .

Why the output is null?

i.e. every time, the word with required length found, that word will be stored in array[0] (Since i is initialized to 0 every iteration of while loop) replacing the previous stored value. As a result, you only get the first value and remaining displayed as null since nothing is stored after array[1].

Why is my array printing out null?

Arrays are objects in Java, so any variable that declares an array holds a reference to an object. If the array hasn't been created yet and you try to print the value of the variable, it will print null (meaning it doesn't reference any object yet).

Why is my String null in Java?

It means the String that does not point to any physical address.” In Java programming language, a “null” String is used to refer to nothing. It also indicates that the String variable is not actually tied to any memory location.


2 Answers

Because the super constructor is first called (super(x)). This super constructor calls the method foo. Then the Derived constructor initializes y to 20 (this.y = y). So when foo is called, y is not initialized yet.

It's a bad practice to call overridable methods in constructors, because, as you just noticed, it can call overridden methods on partially constructed objects.

like image 82
JB Nizet Avatar answered Sep 28 '22 19:09

JB Nizet


The println comes from method foo which is called from Base's constructor, which is called from Derived's constructor (via super) before you initialize y. So the null value is expected.

like image 24
Mat Avatar answered Sep 28 '22 19:09

Mat