This week's book giveaway is in the Python forum. We're giving away four copies of High Performance Python for Data Analytics and have Tiago Rodrigues Antao on-line! See this thread for details.
I need to save a copy of a object so that later I can compare the object with the saved one to see if anything has changed.İs there any simple way to make a copy of an object. My class implements Cloneable,java.io.Serializable but I still can not clone the object.
Cloneable is a funny interface. It contains no methods, but it implies that the clone() method can be called. The weird thing is that clone() is protected in java.lang.Object, so that if all you do is declare that a class implements Cloneable, other classes still can't call clone() on it! What you actually have to do is first, implement Cloneable, and second, override clone() to make it public:
Yes, this is lame. It's just what you have to do. If you've never used clone() before, I should warn you that the default implementation does a shallow copy. Members of primitive type are copied; member of object type are not; the new object has references to the same member objects as the parent did. If you don't like this, you can modify the overridden clone() to do a deep copy.