my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors--THIS() call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors--THIS() call" Watch "Constructors--THIS() call" New topic
Author

Constructors--THIS() call

chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hello friends,

public class Test_Constructor {

int x;
int y;
int z;

//explicit default constructor
/*Test_Constructor() {
x = 10;
y = 20;
z = 30;
System.out.println(x+ "" +y+ "" +z+ " ");
} */
// Non-default constructor

Test_Constructor(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;

System.out.println(x+ "" +y+ ""+z+ " ");
}

Test_Constructor(){
this(10,20,0);
System.out.println(x+ "" +y+ ""+z+ " "); }

public static void main(String[] args) {

Test_Constructor t1 = new Test_Constructor();
Test_Constructor t2 = new Test_Constructor(100,200,300);

}
}

o/p - 10, 20, 0 - this is obvious
10, 20 ,0 - dont know abt this
100,200,300 - this is too

i am getting 3 answers ..can you please explain.

to my mind - 2 outputs are obvious since calling two constructors, but i am confused in the middle one. and can you please explain this() is detail if you can.

- thank you very much
- chintan ramavat
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Let's make your code more readable by enclosing it in [C0DE][/C0DE] tags. I have also removed the confusing commented out version of the no-argument constructor.


o/p - 10, 20, 0 - this is obvious
10, 20 ,0 - dont know abt this
100,200,300 - this is too
[ May 31, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You can perhaps better see that the second line is the output of the System.out.println statement immediately after the call to this(10,20,0).
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi.

The following code:



Calls two System.out.println statements: one inside the no-argument constructor and one inside the constructor with three arguments. That's why you get 10, 20, 0 output twice.


Leandro Melo
SCJP 1.4, SCWCD 1.4
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Thank you so much Berry and Leandro. Please pardon for the way of expressing the problem or any syntax confusion. I just want to add that,Once the No-arg constructor is done with it's values why does it print the other statement of the ARG constructor. Isn't it just completed. may be it's again a dumb question, but can you please explain if you can.

- thanks
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Chintan,
Test_Constructor(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;

System.out.println(x+ "" +y+ ""+z+ " ");
}

Test_Constructor(){
this(10,20,0);
System.out.println(x+ "" +y+ ""+z+ " "); }

public static void main(String[] args) {

Test_Constructor t1 = new Test_Constructor();
Test_Constructor t2 = new Test_Constructor(100,200,300);

}
}

o/p - 10, 20, 0 - this is obvious
10, 20 ,0 - dont know abt this
100,200,300 - this is too

i am getting 3 answers ..can you please explain.

to my mind - 2 outputs are obvious since calling two constructors, but i am confused in the middle one. and can you please explain this() is detail if you can.



Hi look at the comments below

Test_Constructor t1 = new Test_Constructor();

Test_Constructor(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;

System.out.println(x+ "" +y+ ""+z+ " ");//Prints 10,20,0
}

Test_Constructor(){
this(10,20,0);//Calls the overloaded constructor
System.out.println(x+ "" +y+ ""+z+ " "); }//Control comes back and prints 10,20,0

Test_Constructor t2 = new Test_Constructor(100,200,300);//Overloaded constructor is called


Test_Constructor(int x, int y, int z) { //Gets Called
this.x = x;
this.y = y;
this.z = z;

System.out.println(x+ "" +y+ ""+z+ " ");//Prints 100,200,300
}


Regards
Nik
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
I got it, it's a constructor chaining
thanks very much nik,
it helped

- chintan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Constructors--THIS() call
 
Similar Threads
how to compare the values in arrays
Clarification on using increments
Variable initialization mystery...
IrixTech Mock Exam
Doubts on the constructors