Forums Register Login

Constructor Invocation , help me.

+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
When you call this("snootchy"); in thb first constructor it call the second constructor with the parameter and in that there is a call to this(420,"snootchy") so it calls the third constructor and there "third" will be printed and then control transfers to second const where "second" is printed and finally control is transferred to first constructor and "first" is printed
+Pie Number of slices to send: Send
Thanks
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 666 times.
Similar Threads
Need help on constructor
program correction
Checking whether a string is a substring of another string without using indexOf method
How is code executed
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:16:50.