clone : Java Glossary

clone

It can sometimes be faster to make a copy of an existing object than to create a new one and initialise it. The method to do this is called clone(). If you want to make objects of your class publicly cloneable, you need code like this:

You can then use it like this:

Rabbit r1 = new Rabbit();
Rabbit r2 = (Rabbit)r1.clone();

Note: To comply with Cloneable, clone() must return an Object not a Rabbit. In Java version 1.5 or later you can return a Rabbit using the new covariance feature.

Instead of using super.clone(), you could use new Rabbit() and copy the fields one by one to the new object and return the new object. Then you don’t have to mess with CloneNotSupportedException.

If you were happy with a protected clone, that just blindly copied the raw bits of the object, you need not redefine your own version. Usually though, you would want a public one. Note you can’t create a private or default scope clone. You can only increase the visibility when you override.

The default protected clone does a shallow copy, that is, it does not make copies of objects pointed to by the object, but it copies all fields and references in an object. If you write your own clone, you might implement a deep copy, that is, also make copies of objects pointed to, and also objects pointed to by those objects recursively. Be careful. It is easy to create a chain reaction explosion or even an endless loop with a deep copy. Clone returns an Object not the type of the thing cloned. You need to cast it back.

If you are an assembler programmer you can think of clone as implemented by a native method that allocates a block of ram the same size as the existing object, then does a byte block move to copy the object literally byte for byte, then returns a handle to that new ram. I would like to see a variant of this sort of fast object construction for copy constructors, e. g. constructors of the form:

Clever Clone

If you implemented clone in the Dog class, using protected super. clone() in the standard way, like any public method, clone() would be inherited by the Dalmatian class. And further clone of a Dalmatian object would produce a Dalmatian, not a Dog even though clone is defined in Dog.

Cloning Arrays

Learning More

Oracle’s Javadoc on Arrays.deepEquals() : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/clone.html J:\mindprod\jgloss\clone.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.214]
You are visitor number 46,834.