Explanation of output

Posted by Anon on Stack Overflow See other posts from Stack Overflow or by Anon
Published on 2012-12-19T16:56:11Z Indexed on 2012/12/19 17:03 UTC
Read the original article Hit count: 199

Filed under:
|

My program

class Building {
Building() {
    System.out.print("b ");
}

Building(String name) {
    this();
    System.out.print("bn " + name);
}
};

public class House extends Building {
House() {
    System.out.print("h "); // this is line# 1
}

House(String name) {
    this(); // This is line#2
    System.out.print("hn " + name);
}

public static void main(String[] args) {
    new House("x ");
}
}

We know that compiler will write a call to super() as the first line in the child class's constructor. Therefore should not the output be:

b (call from compiler written call to super(), before line#2

b (again from compiler written call to super(),before line#1 )

h hn x

But the output is

b h hn x

Why is that?

© Stack Overflow or respective owner

Related posts about java

Related posts about output