using clone()

Posted on 2006-05-03
Last Modified: 2010-03-31
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?
Question by:inzaghi
    LVL 30

    Expert Comment

    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.
    LVL 30

    Accepted Solution

    For cloning, though, your object must implement Cloneable
    LVL 92

    Assisted Solution

    clone() should be ok, then clear the required fields.
    or create a new bean, and copy over the required properties.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now