<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Clone_%28Java_method%29</id>
	<title>Clone (Java method) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Clone_%28Java_method%29"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Clone_(Java_method)&amp;action=history"/>
	<updated>2026-10-26T04:26:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Clone_(Java_method)&amp;diff=4515177&amp;oldid=prev</id>
		<title>imported&gt;Frap: /* Class hierarchy */ 4-space indention</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Clone_(Java_method)&amp;diff=4515177&amp;oldid=prev"/>
		<updated>2023-06-07T10:51:33Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Class hierarchy: &lt;/span&gt; 4-space indention&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{lowercase}}&lt;br /&gt;
{{More footnotes|date=August 2010}}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;&amp;#039; is a [[Method (computer science)|method]] in the [[Java (programming language)|Java programming language]] for [[Object copy|object duplication]]. In Java, objects are manipulated through reference variables, and there is no operator for &amp;#039;&amp;#039;copying&amp;#039;&amp;#039; an object—the assignment operator duplicates the reference, not the object. The clone() method provides this missing functionality.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
Classes that want copying functionality must implement some method to do so. To a certain extent that function is provided by &amp;quot;&amp;lt;code&amp;gt;Object.clone()&amp;lt;/code&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; acts like a copy constructor. Typically it calls the &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method of its superclass to obtain the copy, etc. until it eventually reaches &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt;&amp;#039;s &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method. The special &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method in the [[Superclass (computer science)|base class]] &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt; provides a standard mechanism for duplicating objects.&lt;br /&gt;
&lt;br /&gt;
The [[class (computer science)|class]] &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt;&amp;#039;s &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method creates and returns a copy of the object, with the same class and with all the fields having the same values. However, &amp;lt;code&amp;gt;Object.clone()&amp;lt;/code&amp;gt; throws a &amp;lt;code&amp;gt;CloneNotSupportedException&amp;lt;/code&amp;gt; unless the object is an instance of a class that implements the [[marker interface pattern|marker interface]] &amp;lt;code&amp;gt;Cloneable&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The default implementation of &amp;lt;code&amp;gt;Object.clone()&amp;lt;/code&amp;gt; performs a [[shallow copy]]. When a class desires a [[deep copy]] or some other custom behavior, they must implement that in their own &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method after they obtain the copy from the superclass.&lt;br /&gt;
&lt;br /&gt;
The syntax for calling &amp;lt;code&amp;gt;clone&amp;lt;/code&amp;gt; in Java is (assuming &amp;lt;code&amp;gt;obj&amp;lt;/code&amp;gt; is a variable of a class type that has a public &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
Object copy = obj.clone();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
or commonly&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
MyClass copy = (MyClass) obj.clone();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
which provides the [[Type conversion|typecast]]ing needed to assign the general &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt; reference returned from &amp;lt;code&amp;gt;clone&amp;lt;/code&amp;gt; to a reference to a &amp;lt;code&amp;gt;MyClass&amp;lt;/code&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
One disadvantage with the design of the &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method is that the return type of &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt;, and needs to be explicitly cast back into the appropriate type. However, overriding &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; to return the appropriate type is preferable and eliminates the need for casting in the client (using [[covariant return type]]s, since J2SE 5.0).&lt;br /&gt;
&lt;br /&gt;
Another disadvantage is that one often cannot access the &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method on an abstract type. Most interfaces and abstract classes in Java do not specify a public &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method. As a result, often the &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method can only be used if the actual class of an object is known, which is contrary to the abstraction principle of using the most generic type possible. For example, if one has a &amp;lt;code&amp;gt;List&amp;lt;/code&amp;gt; reference in Java, one cannot invoke &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; on that reference because &amp;lt;code&amp;gt;List&amp;lt;/code&amp;gt; specifies no public &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method. Actual implementations of &amp;lt;code&amp;gt;List&amp;lt;/code&amp;gt; like &amp;lt;code&amp;gt;ArrayList&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;LinkedList&amp;lt;/code&amp;gt; all generally have &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; methods themselves, but it is inconvenient and bad abstraction to carry around the actual class type of an object.&lt;br /&gt;
&lt;br /&gt;
==Alternatives==&lt;br /&gt;
There are alternatives to &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt;, notably the use of a [[copy constructor]] - a constructor that accepts as a parameter another instance of the same class - or a [[Factory method pattern|factory method]]. These methods are not always adequate when the concrete type of the cloned object is not known in advance. (However, &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; is often not adequate either for the same reason, as most abstract classes do not implement a public &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method.)&lt;br /&gt;
&lt;br /&gt;
Also the use of serialization and deserialization is an alternative to using clone.&lt;br /&gt;
&lt;br /&gt;
==Singleton pattern==&lt;br /&gt;
When 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 &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method using the following code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
    throw new CloneNotSupportedException();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This is only necessary if a superclass implements a public &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method, or to prevent a subclass from using this class&amp;#039;s &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method to obtain a copy. Classes don&amp;#039;t usually inherit a public &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method because &amp;lt;code&amp;gt;Object&amp;lt;/code&amp;gt; doesn&amp;#039;t have a public &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method, so it is usually unnecessary to explicitly implement a non-functional &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method.&lt;br /&gt;
&lt;br /&gt;
==Class hierarchy==&lt;br /&gt;
To 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().&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class X implements Cloneable {&lt;br /&gt;
    public X clone() throws CloneNotSupportedException {&lt;br /&gt;
        return (X) super.clone();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Y extends X { }&lt;br /&gt;
&lt;br /&gt;
public class Z extends Y { }&lt;br /&gt;
&lt;br /&gt;
public class test1 {&lt;br /&gt;
    public void function() throws CloneNotSupportedException {&lt;br /&gt;
        Y varY1 = new Z();&lt;br /&gt;
        Y varY2 = (Y) varY1.clone();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.)&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class X implements Cloneable {&lt;br /&gt;
    public X clone() throws CloneNotSupportedException {&lt;br /&gt;
        return (X) super.clone();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Y extends X { }&lt;br /&gt;
&lt;br /&gt;
public class ObjectABC implements Cloneable {&lt;br /&gt;
    public ObjectABC clone() throws CloneNotSupportedException {&lt;br /&gt;
        return (ObjectABC) super.clone();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Z extends Y {&lt;br /&gt;
    private ObjectABC someABC;&lt;br /&gt;
&lt;br /&gt;
    public Z clone() throws CloneNotSupportedException {&lt;br /&gt;
        Z newZ = (Z) super.clone();&lt;br /&gt;
        newZ.someABC = someABC.clone();&lt;br /&gt;
&lt;br /&gt;
        return newZ;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class test1 {&lt;br /&gt;
    public void function() throws CloneNotSupportedException {&lt;br /&gt;
        Y varY1 = new Z();&lt;br /&gt;
        Y varY2 = (Y) varY1.clone();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Pitfalls ==&lt;br /&gt;
If every class in a hierarchy implements a &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method, all of these functions will be called upon cloning, adding some overhead. Over many iterations, this overhead could become significant.&lt;br /&gt;
&lt;br /&gt;
With complex object graphs, deep copying can also become problematic when recursive references exist.&lt;br /&gt;
&lt;br /&gt;
It is not always appropriate to have multiple copies of the same object floating around. If the purpose of a specific &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; implementation is not fully understood by consumers, it may unintentionally break the &amp;quot;single object, multiple references&amp;quot; paradigm.&lt;br /&gt;
&lt;br /&gt;
==Final fields==&lt;br /&gt;
Generally, &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; is incompatible with &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; fields. Because &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; is essentially a [[default constructor]] (one that has no arguments) it is impossible to assign a &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; field within a &amp;lt;code&amp;gt;clone()&amp;lt;/code&amp;gt; method; a compiler error is the result. Where the value of the field is an [[immutable object]] this is okay; just let the &amp;#039;constructor&amp;#039; copy the reference and both the original and its clone will share the same object.&lt;br /&gt;
&lt;br /&gt;
But where the value is a mutable object it must be deep copied. One solution is to remove the &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; modifier from the field, giving up the benefits the modifier conferred.&lt;br /&gt;
&lt;br /&gt;
For this reason, some programmers suggest to make the objects in the hierarchy [[Serialization#Java|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.&amp;lt;ref&amp;gt;{{cite web |last1=Miller |first1=Dave |date=1999-08-06 |df=mdy |url=https://www.infoworld.com/article/2077578/java-tip-76--an-alternative-to-the-deep-copy-technique.html |title=Java Tip 76: An alternative to the deep copy technique |work=[[JavaWorld]] |accessdate=2020-07-14}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;ref&amp;gt;[https://stackoverflow.com/questions/2427883/clone-vs-copy-constructor-which-is-recommended-in-java Clone() vs Copy constructor- which is recommended in java], [[StackOverflow]]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* {{cite web|url=http://weblogs.java.net/blog/2007/04/04/cloning-java-objects-using-serialization|title=Cloning Java objects using serialization|last=McManus|first=Eamonn|date=April 4, 2007|work=Eamonn McManus&amp;#039;s Blog|publisher=java.net|accessdate=2010-11-16|archive-url=https://web.archive.org/web/20100813191054/http://weblogs.java.net/blog/2007/04/04/cloning-java-objects-using-serialization|archive-date=August 13, 2010|url-status=dead}}&lt;br /&gt;
* {{cite book|last=Bloch|first=Joshua|title=Effective Java: A Programming Language Guide|publisher=Addison-Wesley|year=2008|edition=2nd|series=The Java Series|isbn=978-0-321-35668-0|url-access=registration|url=https://archive.org/details/effectivejava00bloc_0}}&lt;br /&gt;
* {{cite web|url=http://www.javapractices.com/topic/TopicAction.do?Id=71|title=Avoid clone|year=2009|work=Collected Java Practices|publisher=Hirondelle Systems|accessdate=2009-07-31}}&lt;br /&gt;
* {{cite web|url=http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()|title=Object (Java Platform SE 6)|year=2008|work=Java Platform Standard Ed. 6|publisher=Sun Microsystems, Inc.|accessdate=2009-07-31}}&lt;br /&gt;
* {{cite web |last1=Roulo |first1=Mark |date=1999-01-01 |df=mdy |url=https://www.infoworld.com/article/2076332/how-to-avoid-traps-and-correctly-override-methods-from-java-lang-object.html |title=How to avoid traps and correctly override methods from java.lang.Object |work=JavaWorld |accessdate=2020-07-14}} - Covers the basics of implementing the clone method.&lt;br /&gt;
* [https://web.archive.org/web/20130101012249/http://www.javaexperience.com/java-cloning-tutorial/ Java Cloning Tutorial].&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Clone (Java Method)}}&lt;br /&gt;
[[Category:Java (programming language)]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Frap</name></author>
	</entry>
</feed>