Java puzzle 08: Calling constructors within constructors

<< Java Puzzles   Green = Easy,  Blue = Normal,  Red = Hard
 P01   P02   P03   P04   P05   P06   P07   P08   P09   P10   P11   P12   P13   P14   P15   P16   P17   P18   P19   P20   P21   P22   P23   P24 
 By title 
>>

Sometimes, you need to call a constructor within another constructor. Here is an example of how to do that. Try to use a pen and a paper to write notes if you can’t follow all the procedure in your mind clearly.

What is the output of this program and why?

Java puzzle 08: Calling constructors within other constructors

Level: Hard

public class MyConstructor
{
  String x = "f";
  String y = "g";
  
  MyConstructor()
  {
    this("c");
    System.out.print("a");
  }
  MyConstructor(String str)
  {
    this("d", "e");
    this.x = str;
    System.out.print(x);
  }
  MyConstructor(String str, String str2)
  {
    this.y = str2;
    System.out.print(x + "" + y);
  }
  public static void main(String[] args)
  {
    MyConstructor mc = new MyConstructor();
  }
}
Author: Sar Maroof

Answer explanation

  1. By creating the object mc of MyConstructor, the no-argument constructor of the class is called.
  2. The no-argument constructor calls the one-argument constructor.
  3. The one-argument constructor calls the two-argument constructor.
  4. In the block of the two-argument constructor, the value of the x remains the initial value of x, which is f.
  5. In the block of the two-argument constructor, the value of the parameter str2 is assigned to the instance variable y.
  6. In the block one-argument constructor, we passed the values d and e to the two-argument constructor.
  7. So, the statement System.out.print(x + “” + y); writes fe to the standard output.
  8. The second statement in the block one-argument constructor this.x = str assigns the value of c to the variable x. Therefore the statement System.out.print(x); writes c to the standard output.
  9. At last in the no-argument constructor block the statement System.out.print(“a”); writes the letter a to the standard output.
    The correct answer is that this program writes feca to the standard output.

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sarmaroof © 2017