It's not a secret anymore!
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
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: 24193

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

subject: clone interface
It's not a secret anymore!