• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a distinct copy of an Object

 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about clone? Clone will help you create a duplicate of the original object.
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic