Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deep Copies?

 
ryan vines
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could you reform a shallow copy to a deep copy? For example, the field creator is the "Name" of the class type, the reference of the creator is c1 is copyied into c2. Thus c1.getCreator() == c2.getCreator() .
Any advice?
Thanks,
Ryan Vines
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps your question would be clearer if you were to post some more relevant example code.
Concerning the concept of a shallow copy versus a deep copy:
Consider the following (contrived) example.
The big difference between a shallow copy and a deep copy is that in the shallow copy, instance members are reused in the copy. In a deep copy, copies of instance members are used to create the copy.
Note that in the above example, any instance members of bar should also be deep-copied during the deep copy.
 
ryan vines
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, well how would that to apply if you had subclasses and methods to deal with. Take this code for example
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We still have no idea what your question is.
 
ryan vines
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, my question i am asking is how i could change a shallow copy to a deep copy. Like the exapmle i gave above creats shallow copies, not deep. I was wondering how you could change it to make the shallow copies deep copies.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To perform a deep copy, one would normally implement the Object.clone() method, implementing Cloneable .... much as in C++ with the copy constructor.
Inside the clone() method, we would call clone on all the instance variables, resulting in a separate but identical object, with no instance sharing between the two objects.
If, however, some of your instance variables don't implement Cloneable, you would have to manually clone them by using the objects exposed interfaces.
Maybe that would help you a bit. I didn't go through the code you posted. Much too long and way too lazy.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ryan vines:
ok, well how would that to apply if you had subclasses and methods to deal with. Take this code for example [...]

You would need to rework the clone() method like this:

Note that I removed the try..catch block so that CloneNotSupportedException will propagate to the caller. My take is that it's better to fail than return a null from the clone() method as it's expected that a real object will be returned.
 
ryan vines
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Thanks alot guys, i fully understand how deep coping works now. and all that is thanks to you giving me help.
Sincerely,
Ryan Vines
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic