File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors--THIS() call

 
chintan ramavat
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chintan ramavat
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it, it's a constructor chaining
thanks very much nik,
it helped

- chintan
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic