This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "clone interface" Watch "clone interface" New topic

clone interface

Steven Coddington

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

Joined: Jul 08, 2003
Posts: 24189

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: 229
Here is an example of what Ernest describes above:
Shallow Vs. Deep Cloning

I agree. Here's the link:
subject: clone interface