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.
Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Clone an object and compare it with the oldest one

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic