clone method

suggest change

The clone() method is used to create and return a copy of an object. This method arguably should be avoided as it is problematic and a copy constructor or some other approach for copying should be used in favour of clone().

For the method to be used, the class of the object being cloned must implement the Cloneable interface.

The Cloneable interface itself is just a tag interface used to change the behaviour of the native clone() method which checks if the calling object's class implements Cloneable. If that class does not implement this interface a CloneNotSupportedException will be thrown.

The Object class itself does not implement this interface so a CloneNotSupportedException will be thrown if the calling object is of class Object.

For a clone to be correct it should be independent of the object it is being cloned from, therefore it may be necessary to modify the object before it gets returned. This means to essentially create a “deep copy” by also copying any of the mutable objects that make up the internal structure of the object being cloned. If this is not implemented correctly the cloned object will not be independent and have the same references to the mutable objects as the object that it was cloned from. This would result in inconsistent behaviour as any changes to those in one would affect the other.

import java.util.Date;

class Foo implements Cloneable {
    int w;
    String x;
    float[] y;
    Date z;

    public Foo clone() {
        Foo result = new Foo();
        // copy primitives by value
        result.w = this.w;
        // immutable objects like String can be copied by reference
        result.x = this.x;

        // The fields y and z refer to mutable objects; clone them recursively.
        // float[].clone() returns float[]. Date.clone() returns Object and does not
        // declare CloneNotSupportedException, so no try/catch is required here.
        if (this.y != null) {
            result.y = this.y.clone();
        }
        if (this.z != null) {
            result.z = (Date) this.z.clone();
        }

        // Done, return the new object
        return result;
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents