English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

public class Bootchy {
int bootch;
String snootch;
public Bootchy() {
this("snootchy");
System.out.print("first ");
}
public Bootchy(String snootch) {
this(420, "snootchy");
System.out.print("second ");
}
public Bootchy(int bootch,String snootch) {
this.bootch = bootch;
this.snootch = snootch;
System.out.print("third ");
}
public static void main(String[] args)
{
Bootchy b = new Bootchy();
System.out.print(b.snootch + " " + b.bootch);
}
}

Output :
E:\>javac Bootchy.java

E:\>java Bootchy
third second first snootchy 420

2006-08-17 18:39:07 · 1 answers · asked by K Ban 2 in Computers & Internet Programming & Design

1 answers

The reason you get that output is because, when you create the object b in main, you invoke the constructor with no args
That one then invokes the constructor with a string --> this("snootchy");
This 2nd constructor then invokes the 3rd constructor -->
this(420, "snootchy");
The 3rd one exits after assigning those values to member variables and then printing "third"
The 2nd one exits after printig "second"
The 1st one exists after printing "first"
Now the main executes its own print statement printing "snootchy 420"
Hence you see what you see.
Hope this helps!

2006-08-17 19:01:01 · answer #1 · answered by Neil 5 · 0 0

fedest.com, questions and answers