aspose file tools*
The moose likes Beginning Java and the fly likes Question about constructor chaining... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about constructor chaining..." Watch "Question about constructor chaining..." New topic
Author

Question about constructor chaining...

Dennis Hicks
Greenhorn

Joined: Jul 09, 2001
Posts: 12

I'm studying for the SCJP - but CTOR chaining is confusing me. The following code is from A programmers guide to Java certification (great book):

class Light
{
// instance variables

private int noOfWatts;
private boolean indicator;
private String location;
// CTORS
Light()
{
this (0, false);
System.out.println ("Returning from default constructor no. 1 in class Light");
}
Light (int watt, boolean ind)
{
this (watt, ind, "X");
System.out.println ("Returning from non-default constructor no. 2 in class Light");
}
Light (int noOfWatts, boolean indicator, String location)
{
super();
this.noOfWatts = noOfWatts;
this.indicator = indicator;
this.location = new String (location);
System.out.println ("Returning from non-default constructor no. 3 in class Light");
}
}

class TubeLight extends Light
{
// instance variables
private int tubeLength;
private int colorNo;
// CTORS
TubeLight (int tubeLength, int colorNo)
{
this (tubeLength, colorNo, 100, true, "Unknown");
System.out.println ("Returning from non-default constructor no. 1 in class TubeLight");
}
TubeLight (int tubeLength, int colorNo, int noOfWatts, boolean indicator, String location)
{
super (noOfWatts, indicator, location);
this.tubeLength = tubeLength;
this.colorNo = colorNo;
System.out.println ("Returning from non-default constructor no. 2 in class TubeLight");
}
}
public class Chaining
{
public static void main (String[] args)
{
System.out.println ("Creating a new TubeLight object");
TubeLight tubeLightRef = new TubeLight (20, 5);
}
}
I can't understand why the output looks like this:

Creating a TubeLight object.
Returning from non-default constructor no. 3 in class Light
Returning from non-default constructor no. 2 in class TubeLight
Returning from non-default constructor no. 1 in class TubeLight
Why don't I see any of the prints from CTORs 1 and 2 in the Light class?

Cheers for any insights -
Den
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4699
    
    7


Why don't I see any of the prints from CTORs 1 and 2 in the Light class?

Because they don't get called. I have added comments to show the sequence that the flow of control follows. The 'super();' statement in Light constructor # 3 is calling the Object class' no-args constructor, not the Light() no-args constructor, so Light constructors 1 & 2 are not called.

<pre>
class Light {
// instance variables

private int noOfWatts;
private boolean indicator;
private String location;

// CTORS
Light() {
this(0, false);
System.out.println("Returning from default constructor no. 1 in class Light");
}

Light(int watt, boolean ind) {
this(watt, ind, "X");
System.out.println(
"Returning from non-default constructor no. 2 in class Light");
}

// ** 6 **
Light(int noOfWatts, boolean indicator, String location) {
super();
this.noOfWatts = noOfWatts;
this.indicator = indicator;
this.location = new String(location);
System.out.println(
"Returning from non-default constructor no. 3 in class Light");
}
}

class TubeLight extends Light {
// instance variables
private int tubeLength;
private int colorNo;
// CTORS

// ** 2 **
TubeLight(int tubeLength, int colorNo) {
this(tubeLength, colorNo, 100, true, "Unknown"); // ** 3 **
System.out.println(
"Returning from non-default constructor no. 1 in class TubeLight");
}

// ** 4 **
TubeLight(
int tubeLength,
int colorNo,
int noOfWatts,
boolean indicator,
String location) {
super(noOfWatts, indicator, location); // ** 5 **
this.tubeLength = tubeLength;
this.colorNo = colorNo;
System.out.println(
"Returning from non-default constructor no. 2 in class TubeLight");
}
}

public class Chaining {
public static void main(String[] args) {
System.out.println("Creating a new TubeLight object");
TubeLight tubeLightRef = new TubeLight(20, 5); // ** 1 **
}
}
</pre>
[This message has been edited by JUNILU LACAR (edited July 09, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Dennis Hicks
Greenhorn

Joined: Jul 09, 2001
Posts: 12
That's great, thanks for pointing it out and showing the flow of execution.
For some reason I thought that super() would be calling the Light class no-args constructor, but of course, it isn't! ;-)
Stupid brain....
Thanks again -
Den
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about constructor chaining...