• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem i faced in cloning

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code for cloning objects
class demo implements Cloneable
{
int x=5;
int y=10;
public demo cloning() throws CloneNotSupportedException
{
return (demo)super.clone();
}
}
class clonedemo
{
public static void main(String args[]) throws CloneNotSupportedException
{
demo d1=new demo();
demo d2= d1.cloning();
System.out.println("d1.x:" +d1.x +"d1.y:" +d1.y);
System.out.println("d2.x:" +d2.x +"d2.y:" +d2.y);
d2.x=1000;
d2.y=2000;
System.out.println("d2.x:" +d2.x +"d2.y:" +d2.y);
System.out.println("d1.x:" +d1.x +"d1.y:" +d1.y);
}
}
output:
d1.x:5 d1.y:10
d2.x:5 d2.y:10
d2.x:1000 d2.y:2000
d1.x:5 d1.y:10
Eventhough the object d2 is cloned properly, Why is that the last two SOP(print) statements are not the same.Please help me in
this Regard. Thank you
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing wrong with your code or the output, it's exactly what to expect. Cloning creates a complete new object that has the same values of the original. After you cloned d1 to d2, they both have the same values. When you modify d2, since it is a seperate object, that in no way affects the values of d1.
Hope this helps
 
sathish gopal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if u r cloning an object it'll make a bitwise copy of it. so any changes in the cloning will affect the original.that is the use of cloning.but here i'm changing the values of variables associated with the clone object . But the values of the original object are not being changed. why ? or give me the correct use of cloning.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right when you say that cloning makes a copy. Note the word "copy". When you change the "copy" it gets changed. But since its a separate thing / object, the original doesn't get changed. As simple as that.
Muhammad Ali Shah
FAST ICS, Karachi.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic