• Post Reply Bookmark Topic Watch Topic
  • New Topic

can someone explain the output of copy constuctor  RSS feed

 
abhinav sood
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Temp2
{
int x;
int y;
Temp2(int x,int y)
{
this.x=x;
this.y=y;
}
void show()
{
System.out.println(x);
System.out.println(y);
}
Temp2( Temp2 z)
{
this.x=x;
this.y=y;
}
public static void main(String... s)
{
Temp2 t1=new Temp2(10,20);
t1.show();
Temp2 t2=new Temp2(t1);
t2.show();
}
}
output: 10,20,0,0
 
Deepak Rao
Ranch Hand
Posts: 36
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhinav,

For your T2 instance, x and y are not set. Hence when you call Temp2( Temp2 z) constructor it sets x,y to zero.
Rewrite your second contructor as

Temp2( Temp2 z)
{
this.x=z.x;
this.y=z.y;
}

Thanks
Deepak

Note: Why is this under JDBC. Please post them under the right forums.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!