This article provided by MiniTool official gives a brief review of the Java clone and introduces its meaning, method, merits, defects, as well as cloning types. Read it through and learn more about the clone () method in the JAVA language.
What Is Java Clone?
Java clone, clone(), refers to the clone method in Java programming language for copying an object. In Java, an object is manipulated via reference variables. There is no operator for duplicating objects. The assignment operator copies the reference instead of the object. Therefore, the Java clone method functions as the duplication feature of the object.
About Java Clone() Method
Classes that intend to copy functionality must implement some method to achieve that. To a certain extent, that kind of function is offered by Object.clone(). Clone () functions as a copy constructor. Typically, it calls the clone method of its superclass to gain the copy, etc. until it finally gets the Object.cloen() method.
The special clone method in the base class Object offers a standard mechanism for copying objects. The class object’s clone method makes and returns a copy of the object with the same class as well as all the fields of the same values.
Yet, Object.clone() throws a CloneNotSuppertedException unless the object is an instance of a class, which implements the marker interface Clonable. The default implementation of Object.clone() performs a shallow copy. If a class wants a deep copy or some other custom behavior, they have to implement that in their own Java clone method once they get the copy from the superclass.
The syntax to call clone in Java is as below:
Object copy = obj.clone();
OR
MyClass copy = (MyClass) obj.clone();
The latter clone command provides the typecasting needed to assign the general object reference returned from clone to a reference to a MyClass object.
Java Clone Alternatives
In general, there are 2 java clone alternatives, copy constructor and factory method. The copy constructor is a constructor that accepts as a parameter another instance of the same class.
Both alternatives are not always adequate when the concrete type of the Java clone object is not known in advance. Yet, the clone method isn’t often adequate either for the same reason that most abstract classes don’t implement a public clone method.
Java Clone Advantages and Disadvantages
Compared with the copy constructor and assignment operator, the clone method in Java has both pros and cons.
Advantages of Java Clone
- If you make use of the copy constructor, you have to copy all of the data over explicitly. While in the clone method, making a new copy is done by the method itself without an extra object clone process. So, cloning needs fewer lines of code.
- Cloning is the easiest way to copy objects, especially for an already developed or old project.
- Clone is also the fastest way to copy arrays.
- If you rely on the assignment operator to assign an object reference to another reference variable. It will point to the same address location of the old object without a new copy of the object be created. Thus, any changes in the reference variable will be reflected in the original object even if you don’t want them to.
Disadvantages of Java Clone
Though Java cloning is useful and can directly copy an object, it still has some disadvantages.
On the one hand, the return type of clone is the object. So, the return type needs to be explicitly cast back to the appropriate type. Since J2SE 5.0, relying on the covariant return types to override clone to return to the appropriate type is preferable and eliminates the need for casting the client.
On the other hand, you can’t reach the clone method on an abstract type. Most abstract classes and interfaces in Java don’t specify a public clone method. Thus, usually, the clone method can only be accessed if the actual class of an object is known, which is contrary to the abstraction principle of using the most generic type possible.
For instance, if you have a List reference in Java, you can’t invoke the clone method on that reference for List specifies no public clone method. Actual implementations of List almost all have clone methods, such as ArrayList and Linked List. Yet, it’s a bad abstraction and inconvenient to carry out the actual class type of an object.
There are still many other disadvantages
- You can’t control object construction for clone() doesn’t invoke any constructor.
- You can’t manipulate final fields in clone() for final fields can only be changed by constructors.
- clone() is protected so that you have to provide your own clone method and indirectly call Object.clone() from it.
- clone() only supports shallow cloning. So, the reference fields of your newly cloned object still hold objects whose fields of your original object was holding.
- If writing a clone method into a child class, all of its superclasses should define the clone method in them or inherit the clone method from other parent classes.
- To use the clone method, you need to add a lot of syntax to your code including calling clone() and casting it on your object, defining the clone method, implementing a cloneable interface, and handling CloneNotSuppertedException.
Shallow Clone vs Deep Clone
The hallow copy is a method of duplicating an object. It is followed by default in the JAVA clone. Shallow copy is a simple and cheap method. While a deep copy copies all fields. It creates copies of dynamically allocated memory pointed to by the fields. A deep Java clone occurs when an object is copied along with the objects to which it refers.
You May Also Like