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

I got a program from my friend, it was ok. When i compiled it and ran it,
i got a very different output.
I am pasting the program with output. Please tell me how constructors got invoked in respective program.
File : Bootchy.java

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:55:13 · 2 answers · asked by K Ban 2 in Computers & Internet Programming & Design

2 answers

Constructor in the same class have same priority

its bcoz of this

On the first constructor u try to call the second constructor first before the System.out.println("first") so it will call the second constructor.

second constructor again got a call to third constructor

So third constructor doesn't have anything to do and

just print the thing out and return the call to second constructor then the first constructor


Everything is rite


Don't worry The first constructor is the constructor has executed first Unless or untill ther is no other explicit call for another constructor from the main method

2006-08-17 19:08:05 · answer #1 · answered by Listen 1 · 0 0

I answered this question for you in your previous post. Go see it there!

2006-08-17 19:02:31 · answer #2 · answered by Neil 5 · 0 0

fedest.com, questions and answers