In terms of "best practices", which methodology is preferred for creating a "deep copy" of an object?
Use a copy constructor. Cloneable is a straight-up API disaster. See Effective Java Item 10 (Item 11 in the 2nd. ed.).
Item 11: Override
clonejudiciouslyThe
Cloneableinterface was intended as a mixin interface (item 18) for objects to advertise that they permit cloning. Unfortunately, it fails to serve this purpose. Its primary flaw is that it lacks aclonemethod, andObject'sclonemethod is protected. You cannot, without resorting to reflection (Item 53), invoke theclonemethod on an object merely because it implementsCloneable. Even a reflective invocation may fail, as there is no guarantee that the object has an accessibleclonemethod.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With