• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor Invocation , help me.  RSS feed

 
Tanka Banerjee
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Ananth Majumdar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tanka Banerjee
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!