• 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 in Cloning dataobject - Urgent Please

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem in cloning a nested dataobject.I get a nested dataobject which needs to be cloned.Structure of my nested object as follows:

I clone as below :

Looks like, SmallDataObject value is getting changed both in cloned and original objects.But the BigDataObject value is not changed
How can I avoid my original Data Object is getting modified when I mdify my clone??
Thanks
sri
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First let me say, good poster! Rather then just say "urgent" you also put in a subject. That deserves an answer.
The problem is that you're doing a shallow copy and not a deep copy. This is noted in the Javadoc for Object.
See http://www.cs.dartmouth.edu/~farid/teaching/cs15/cs5/lectures/0512/0512.html for more info on cloning.

--Mark
 
Author
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the previous post about it being a shallow copy is exactly correct. 99 times out of 100 when I want to use someObject.clone() I **DO** want a deep copy. So far, the easiest technique I've developed to do this is as follows: (1) make sure that all my objects are Serializable. (2) override the clone() method using Data[Input,Output]Stream objects to serialize/deserialize the object. That will always return a deep copy and I don't have to worry about screwing up the .clone() method if I add/remove properites of the object. Here's my clone() method:<br><br>
<xmp>
/**
* create a deep copy of this object by serializing it to
* a byte stream and then de-serializing it.
*/
public Object clone()
{
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
oos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
Object o = ois.readObject();
bais.close();
ois.close();
baos.close();
oos.close();
return o;
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
</xmp>
 
Proudly marching to the beat of a different kettle of fish... while reading 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