Is it possible to deep copy an Object out of the box? i.e. any other way than coding a clone function manually.
Is it possible to deep copy an Object out of the box? i.e. any
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Cloning does not necessarily perform a deep copy. In fact, the default implementation of
Object.clone()creates a shallow copy.If the object’s closure consists of objects that implement
SerializableorExternalizable, you can useObjectOutputStreamandObjectInputStreamto create a deep copy … but it is expensive.The
cloninglibrary is another option, but my initial reading of the code is that it relies on the class of every object in the graph providing a no-argument constructor. Then it will then patch the resulting object to have a copy of the original object’s state. This process might have undesirable side-effects, depending on what the no-args constructor actually does.In short, I don’t think there is a universal solution.