Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

using clone()

I have a dto object which is populated by a Data Acess object.
I need a exact copy of this object as I need to re-insert this data back into
the data-tier. I only need to change the unique id - the primary key field and one other field.

Is it ok to use the clone() method?
  • 2
2 Solutions
Mayank SAssociate Director - Product EngineeringCommented:
While using clone (), be a little careful because it will clone the references. Meaning that, it will be a shallow copy. Basically the data-members of the cloned (new) object will be referring to the same objects where the data-members of the initial object were referring. If you have any collections in your data-members and you want to clone them too, you need a deep-copy.
Mayank SAssociate Director - Product EngineeringCommented:
For cloning, though, your object must implement Cloneable
clone() should be ok, then clear the required fields.
or create a new bean, and copy over the required properties.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now