• 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

clone() method confusion

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys..
Please take a look at this code:

aClass a = new aClass();
aClass b = (aClass) a.clone();

System.out.println(a.some_var);
System.out.println(b.some_var); // both prints the same value.

a.some_var = 1234; // changes the original value.

System.out.println(a.some_var); // prints the new value.
System.out.println(b.some_var); // prints the original value.

I understand that clone() creates a new instance and copies the values of the clones object. SO when I change values in one object it should not affect the other. So the above code is ok.

BUT, in the bonus exam I got from "Sybex Complete Java Certification" book, it states:

"When you clone an object all the data is copied to the new object.Consequntly Obj_2 (a cloned object) has the same data and references as Obj_1 (the object which was cloned from), and a change to Obj_2's data will result in a change to Obj_1's data."

Can somebody please explain me this contradictory behavior of the program and this statement.

Thank you.
Regards,
Maduranga.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If i remember my basics right, i would say that the primitives will be copied from the original object to the cloned object. So when you change the primitive in the original object it doesnot get reflected in the cloned object. But the references in the original object is not be copied ( unless you do a deep copy ) to the cloned object and they will point to the same objects. So when you change the data, that will get reflected in the cloned object since they point to the same references. Cloning usually does shallow copy. Hope this helps.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do u mean by deep copy ?

pls give some explanation with example

thanx
 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Reghu..

Thanks for the reply. I think I get it now. So if i do a 'deep copy', new objects will be created and the references of those new objects will be crated to the cloned object right?
Because I'm only doin a 'shallow copy', only the original object references will be copied to the cloned copy.

Thank you.
Regards,
Maduranga.
 
Reghu Ram Thanumalayan
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Liyanage,
You got it right.

For shallow vs deep copy, see this link.Shallow vs Deep Copy

Also this discussion Javaranch Discussion
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic