• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make a copy of an object without using Object.clone()?

 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to make a copy of an object without using Object.clone()?
Thanks!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can serialize it, there might be a way. I don't remember the details though. Otherwise, you out of luck.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you facing some technical issue in using clone or is this a Quiz kind of question?
 
Bruce Jin
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes 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. I am thinking there maybe a simple way to make a copy of a object without implementing Clonable and override clone() in my class.
Thanks
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bruce Jin:
Sometimes 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. I am thinking there maybe a simple way to make a copy of a object without implementing Clonable and override clone() in my class.
Thanks


Check out the Memento pattern in the GoF book.
Must add that it is not a simple solution (but worth the while considering it).
-GB.
[ February 04, 2003: Message edited by: Gopi Balaji ]
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another option is using the ObjectOutputStream to write the object to disk, then read it back in when you need to compare into a new object. Might be a performance issue though.
 
reply
    Bookmark Topic Watch Topic
  • New Topic