Deep cloning of Serializable objects

Java1 Comment on Deep cloning of Serializable objects

Deep cloning of Serializable objects

The following code snippet shows how to use Object I/O streams to deep clone Serializable objects. The object bytes once written on an ObjectOutputStream can be read back to a new object with all the populated values intact. The only catch is that, because the object is written and then read from a stream, this process is slower then the normal getter-setter approach.

One thought on “Deep cloning of Serializable objects

  1. This is exactly the approach I have taken :). I wanted a general utility to deep clone an entire object (graph) based at a business entity object. I took the easy approach of using apache.commons.lang's SerializationUtils; the code becomes:@SuppressWarnings("unchecked") public static T deepClone(T object) { return (T) deserialize(serialize(object)); }Unfortunately as you pointed out this is very slow.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
Fork me on GitHub