| Author |
a distinct copy of an Object
|
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
i have an object which contains values which are of type File, boolean, String, int. I wish to make a distinct copy. Is there a way to code this dynamically. For all elements in objectOriginal: objectCopy = objectOriginal(i) or must I hard code (and maintain) the copy to reflect the actual object objectCopy.file = objectOrigianl.file Thanks!
|
 |
satishkumar janakiraman
Ranch Hand
Joined: May 03, 2004
Posts: 334
|
|
|
What about clone? Clone will help you create a duplicate of the original object.
|
 |
Norm Radder
Ranch Hand
Joined: Aug 10, 2005
Posts: 681
|
|
I think you have to override the clone method to do deep copying. Read the API Object.clone() method doc for more details.
|
 |
Robert Kennedy
Ranch Hand
Joined: Jun 27, 2008
Posts: 63
|
|
I have read that the clone method is somewhat unreliable (at least for objects). For the present I will create a copy method in the class. Thanks
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Agree with Norm Radder. That is the Java way to do it, even though the clone() method may be conceptually flawed. Do a search for sample chapters of Joshua Bloch "Effective Java." There is a sample chapter somewhere on the Sun website, but I can't remember where. That sample chapter covers the clone() method.
|
 |
 |
|
|
subject: a distinct copy of an Object
|
|
|