0
class parent {
    String s;

    parent() {
         this(10);
         this.s = "First";
    }
    parent(int a){
         this(10.00f);
         this.s += "Second";
    }
    parent(float b){
         this("sss");
         this.s += "Third";
    }
    parent(String c){
         this('a');
         this.s += "Fourth";
    }
    parent(char d){
         this.s = "Fifth"; 
         System.out.println(this.s);
    }
}

class chain {
    static public void main(String[] string) {
        parent p = new parent();
    }
}

The output is

Fifth

I expected the following would be the flow

parent()->parent(int a)->parent(float b)->parent(String c)->parent(char d).

This happens but once the last constructor is executed I thought the remaining String, float, int and no-arg constructor variants would be executed because they do have code to process and is it not how they work.

I assume that constructors' execution is stack based (correct me if I am wrong).

2
  • 1
    It is stack based. Add System.out.println(this.s); to each constructor and see the result. Commented Sep 1, 2013 at 3:06
  • System.out.println missing and this.s += are the culprits..Tx Eric/lulyon Commented Sep 1, 2013 at 3:07

3 Answers 3

4

You're right and wrong.

The remaining code in the other constructors does execute, but you print out the value of this.s before they do.

Here is the flow of your code, in vertical chronological order:

parent()
  this(10)
    this(10.00f)
      this("sss")
        this('a')
          this.s = "Fifth"
          System.out.println(this.s)
        this.s += "Fourth"
      this.s += "Third"
    this.s += "Second"
  this.s = "First"

You'll either need to print out p.s after new parent(), or move your print statement to the end of parent() (and change = "First" to += "First").

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Eric..i got it now
1

Edit Eric pointed out the actual issue, which is that you are printing out this.s after it has only been modified once. Also as he says, if you had += in all the right places, they would be concatenated in the opposite order of what you are expecting.

Note that there is a major problem with these constructors in that if you use any one other than the first, it will error out due to a null reference. You should have:

if(this.s == null){
   this.s = "";
}

at the beginning of all the constructors except the first.

2 Comments

this.s = "Fifth" is executed before any of the += statements are, so I doubt this is the issue.
I agree with Eric. Because when I keep this.s += "Fifth"..I get nullFifth as the output.
0

You're only seeing Fifth because your System.out.println() call is right after this.s = "Fifth";

If you add a System.out.println after every addition to s, you get:

Fifth
FifthFourth
FifthFourthThird
FifthFourthThirdSecond
First

First resets it because you used = there instead of +=. Fix that, and you get:

Fifth
FifthFourth
FifthFourthThird
FifthFourthThirdSecond
FifthFourthThirdSecondFirst

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.