| Author |
can someone explain the output of copy constuctor
|
abhinav sood
Greenhorn
Joined: Aug 05, 2011
Posts: 9
|
|
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
Joined: Jan 24, 2012
Posts: 31
|
|
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.
|
 |
 |
|
|
subject: can someone explain the output of copy constuctor
|
|
|