• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

can someone explain the output of copy constuctor

 
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
 
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!