This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes clone interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "clone interface" Watch "clone interface" New topic
Author

clone interface

Steven Coddington
Greenhorn

Joined: Oct 17, 2004
Posts: 10
I need to do a deep copy of an object with both primative and String type variables. "clone" returns a copy with values for primatives and references for non-primative String ( I think). Is there an alternative to clone() that does deep, or can someone help me write an alternative.


Steve Coddington
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

You can override clone() with your own version with deep-copy semantics. Depending on the class, this might be as simple as calling super.clone() to get a shallow copy, and then calling clone() on each of your member objects, installing the copies inside your shallow copy, and then returning it.


[Jess in Action][AskingGoodQuestions]
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 224
Here is an example of what Ernest describes above:
Shallow Vs. Deep Cloning

Regards,
Saket
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: clone interface
 
Similar Threads
clone() used on array and object
Copy Constructor concept n Java
how to deep copy a linkedlist?
cloning question
need your help about a cast about cloneable.!!!(jcjp07-Q119)