aspose file tools
The moose likes Beginning Java and the fly likes can someone explain the output of copy constuctor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "can someone explain the output of copy constuctor" Watch "can someone explain the output of copy constuctor" New topic
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
 
Threads others viewed
equal() and ==
Serialization code Issue
Constructors--THIS() call
does every object have its own method
Confused help me!!!!!!
developer file tools