Nick Carroll

Metabolising caffeine into code

Archive for the ‘Programming’ tag

Cloning a Java object

without comments

Here is a simple and effective approach to cloning objects in Java using XStream. The process uses XStream to serialize your object to XML, and then using the XML to create a new Java object that is a deep copy of the original.

Sheep dolly = new Sheep("Dolly");
XStream xstream = new XStream();
String xml = xstream.toXML(dolly);
Sheep newDolly = (Sheep)xstream.fromXML(xml);

Written by Nick

January 15th, 2010 at 12:09 pm

Posted in Programming

Tagged with ,