Home » questions » Constructor Invocation , help me.?

Constructor Invocation , help me.?

2006-08-17 22:55:13, Category: Programming & Design
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

Answers

  1. Listen

    On 2006-08-17 23:08:05


    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
  2. Neil

    On 2006-08-17 23:02:31


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