HOME

TheInfoList



OR:

clone() is a
method Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
in the
Java programming language Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
for object duplication. In Java, objects are manipulated through reference variables, and there is no operator for ''copying'' an object—the assignment operator duplicates the reference, not the object. The clone() method provides this missing functionality.


Overview

Classes that want copying functionality must implement some method to do so. To a certain extent that function is provided by "Object.clone()". clone() acts like a copy constructor. Typically it calls the clone() method of its superclass to obtain the copy, etc. until it eventually reaches Object's clone() method. The special clone() method in the
base class In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new classe ...
Object provides a standard mechanism for duplicating objects. The
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
Object's clone() method creates and returns a copy of the object, with the same class and with all the fields having the same values. However, Object.clone() throws a CloneNotSupportedException unless the object is an instance of a class that implements the
marker interface The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support ...
Cloneable. The default implementation of Object.clone() performs a
shallow copy In object-oriented programming, object copying is creating a copy of an existing object, a unit of data in object-oriented programming. The resulting object is called an ''object copy'' or simply ''copy'' of the original object. Copying is basic bu ...
. When a class desires a deep copy or some other custom behavior, they must implement that in their own clone() method after they obtain the copy from the superclass. The syntax for calling clone in Java is (assuming obj is a variable of a class type that has a public clone() method): Object copy = obj.clone(); or commonly MyClass copy = (MyClass) obj.clone(); which provides the
typecast In film, television, and theatre, typecasting is the process by which a particular actor becomes strongly identified with a specific character, one or more particular roles, or characters having the same traits or coming from the same social or ...
ing needed to assign the general Object reference returned from clone to a reference to a MyClass object. One disadvantage with the design of the clone() method is that the return type of clone() is Object, and needs to be explicitly cast back into the appropriate type. However, overriding clone() to return the appropriate type is preferable and eliminates the need for casting in the client (using
covariant return type In object-oriented programming, a covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden in a subclass. A notable language in which this is a fairly common paradigm is C++. C# supports retu ...
s, since J2SE 5.0). Another disadvantage is that one often cannot access the clone() method on an abstract type. Most interfaces and abstract classes in Java do not specify a public clone() method. As a result, often the clone() 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 List reference in Java, one cannot invoke clone() on that reference because List specifies no public clone() method. Actual implementations of List like ArrayList and LinkedList all generally have clone() methods themselves, but it is inconvenient and bad abstraction to carry around the actual class type of an object.


Alternatives

There are alternatives to clone(), notably the use of a
copy constructor In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required ...
- a constructor that accepts as a parameter another instance of the same class - or a
factory method In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by cr ...
. These methods are not always adequate when the concrete type of the cloned object is not known in advance. (However, clone() is often not adequate either for the same reason, as most abstract classes do not implement a public clone() method.) Also the use of serialization and deserialization is an alternative to using clone.


Singleton pattern

When writing a class using the
Singleton pattern In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. One of the well-known "Gang of Four" design patterns, which describe how to solve recurring problems ...
, 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 clone() method using the following code: public Object clone() throws CloneNotSupportedException This is only necessary if a superclass implements a public clone() method, or to prevent a subclass from using this class's clone() method to obtain a copy. Classes don't usually inherit a public clone() method because Object doesn't have a public clone() method, so it is usually unnecessary to explicitly implement a non-functional clone() method.


Class hierarchy

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(). 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 class Y extends X public class Z extends Y public class test1 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 class Y extends X public class ObjectABC implements Cloneable public class Z extends Y public class test1


Pitfalls

If every class in a hierarchy implements a clone() method, all of these functions will be called upon cloning, adding some overhead. Over many iterations, this overhead could become significant. 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 clone() implementation is not fully understood by consumers, it may unintentionally break the "single object, multiple references" paradigm.


Final fields

Generally, clone() is incompatible with final fields. Because clone() is essentially a
default constructor In computer programming languages, the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a nullary constructor. I ...
(one that has no arguments) it is impossible to assign a final field within a clone() method; a compiler error is the result. Where the value of the field is an
immutable object In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created.Goetz et al. ''Java Concurrency in Practice''. Addison Wesley Professional, 2006, Section 3 ...
this is okay; just let the 'constructor' copy the reference and both the original and its clone will share the same object. But where the value is a mutable object it must be deep copied. One solution is to remove the final modifier from the field, giving up the benefits the modifier conferred. 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 A bitstream (or bit stream), also known as binary sequence, is a sequence of bits. A bytestream is a sequence of bytes. Typically, each byte is an 8-bit quantity, and so the term octet stream is sometimes used interchangeably. An octet may ...
, which handles final data members correctly, but is significantly slower. 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. Clone() vs Copy constructor- which is recommended in java
StackOverflow In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many facto ...


References


External links

* * * * * - Covers the basics of implementing the clone method.
Java Cloning Tutorial
{{DEFAULTSORT:Clone (Java Method) Java (programming language)