Clone (Java method)
OverviewClasses that want copying functionality must implement some method to do so. To a certain extent that function is provided by "
The class The default implementation of The syntax for calling Object copy = obj.clone();
or commonly MyClass copy = (MyClass) obj.clone();
which provides the typecasting needed to assign the general One disadvantage with the design of the Another disadvantage is that one often cannot access the AlternativesThere are alternatives to Also the use of serialization and deserialization is an alternative to using clone. Singleton patternWhen writing a class using the Singleton pattern, only one instance of that class can exist at a time. As a result, the class must not be allowed to make a clone. To prevent this, one can override the public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
This is only necessary if a superclass implements a public Class hierarchyTo provide a properly cloneable object of any type, the clone() method must be both declared correctly and implemented correctly according to the convention described in Object.clone(). 1) Every type that needs to be cloned must have a public clone() method in its own class or a publicly accessible clone() method in one of its parent classes. Example: To invoke clone() on varY1, which is of type Y, then Y or a parent of Y must declare a publicly accessible clone() method. Here, it is the parent class X that provides the public clone() method. public class X implements Cloneable {
public X clone() throws CloneNotSupportedException {
return (X) super.clone();
}
}
public class Y extends X { }
public class Z extends Y { }
public class test1 {
public void function() throws CloneNotSupportedException {
Y varY1 = new Z();
Y varY2 = (Y) varY1.clone();
}
}
2) Every class that implements clone() should call super.clone() to obtain the cloned object reference. If the class has any object references that must be cloned as well (when deep copying, for example), then the clone() method should perform any required modifications on the object before returning it. (Since Object.clone() returns an exact copy of the original object, any mutable fields such as collections and arrays would be shared between the original and the copy - which in most cases would neither be expected nor desired.) Example: Since class Z contains an object reference, its clone() method also clones that object reference in order to return a deep copy of the original. public class X implements Cloneable {
public X clone() throws CloneNotSupportedException {
return (X) super.clone();
}
}
public class Y extends X { }
public class ObjectABC implements Cloneable {
public ObjectABC clone() throws CloneNotSupportedException {
return (ObjectABC) super.clone();
}
}
public class Z extends Y {
private ObjectABC someABC;
public Z clone() throws CloneNotSupportedException {
Z newZ = (Z) super.clone();
newZ.someABC = someABC.clone();
return newZ;
}
}
public class test1 {
public void function() throws CloneNotSupportedException {
Y varY1 = new Z();
Y varY2 = (Y) varY1.clone();
}
}
PitfallsIf every class in a hierarchy implements a With complex object graphs, deep copying can also become problematic when recursive references exist. It is not always appropriate to have multiple copies of the same object floating around. If the purpose of a specific Final fieldsGenerally, But where the value is a mutable object it must be deep copied. One solution is to remove the For this reason, some programmers suggest to make the objects in the hierarchy Serializable, and create copies by serializing the old object and then creating a new object from the resulting bitstream, which handles final data members correctly, but is significantly slower.[1] Alternatively, one can return a completely new object from the current objects fields, which can be done first calling the constructor, and later assigning non final fields. Another alternative method is actually making the idea formal : creating a copy constructor that takes an instance. In fact that is what is recommended over clone by some people. [2] References
External links
Information related to Clone (Java method) |