What is the better way of writing the Constructor with 2 parameters in the following code:class Test { int x,y; Test(int a) { //Code for very complex operations will be written //in this place x=a; } Test(int a, int b) { //Code for very complex operations will be written //in this place (same code as in above constructor) x=a; y=b; } }

Showing Answers 1 - 3 of 3 Answers

wtf

  • May 2nd, 2005
 

I cant understand what they mean with that question. 
Who knows the answer?

  Was this answer useful?  Yes

Hari

  • May 9th, 2005
 

Test(int a, int b) { 
this(a); 
this.y = b; 
}

  Was this answer useful?  Yes

Sumit Sengar

  • Aug 2nd, 2005
 

Test(int a, int b) 

this(a); 

 
This is becoz assigning the value a to instance variable has already been done in one place so why to repeat it again in this constructor also.So the better way is to call the first 1 argument constructor using this keyword.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions