Search code examples
javadeep-copy

How to make deep copy of java object without using serialization ?


Is it possible to make a deep copy/clone of a Java object without using serialization ? If so then how ?


Solution

  • You could use the Java Deep-Cloning Library to make deep copies of objects. It is really useful when you can't (or don't want) to make your classes serializable. The use is straight-forward:

    Cloner cloner = new Cloner();
    
    MyClass clone = cloner.deepClone(o);
    // clone is a deep-clone of o