How do you make a deep copy of an object in Java?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In java it's a bit difficult to implement a deep object copy function. What steps you take to ensure the original object and the cloned one share no reference? 回答1: A safe way is to serialize the object, then deserialize. This ensures everything is a brand new reference. Here's an article about how to do this efficiently. Caveats: It's possible for classes to override serialization such that new instances are not created, e.g. for singletons. Also this of course doesn't work if your classes aren't Serializable. 回答2: A few people have