This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to make a copy of an object without using Object.clone()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to make a copy of an object without using Object.clone()?" Watch "How to make a copy of an object without using Object.clone()?" New topic
Author

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

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
How to make a copy of an object without using Object.clone()?
Thanks!


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Simon Percivall
Greenhorn

Joined: Jul 25, 2002
Posts: 3
If you can serialize it, there might be a way. I don't remember the details though. Otherwise, you out of luck.
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
Are you facing some technical issue in using clone or is this a Quiz kind of question?


<B>Hari Gangadharan</B><BR>Unix is user friendly..<BR>but it chooses to whom it is friendly with!
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
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
Gopi Balaji
Ranch Hand

Joined: Jan 23, 2003
Posts: 84
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 ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make a copy of an object without using Object.clone()?
 
Similar Threads
Cloneable interface and Array
class Class and class literals
clone() used on array and object
Java Clone
What is the difference between the deep copy and shallow copy in Object.clone()