Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Cloneable work in Java and how do I use it?

Tags:

java

cloneable

I would like to know the following:

  1. Cloneable means we can have a clone or a copy of objects, by implementing the Cloneable interface. What are the advantages and disadvantages of doing that?
  2. How does the recursive cloning happen if the object is a composite object?
like image 432
daydreamer Avatar asked Nov 02 '10 20:11

daydreamer


People also ask

What does cloneable do in Java?

Cloneable is an interface that is used to create the exact copy of an object. It exists in java. lang package. A class must implement the Cloneable interface if we want to create the clone of the class object.

How does cloneable interface work?

Cloneable interface is implemented by a class to make Object. clone() method valid thereby making field-for-field copy. This interface allows the implementing class to have its objects to be cloned instead of using a new operator.

How does clone () work java?

clone() method acts like a copy constructor. It creates and returns a copy of the object. Since the Object class has the clone method (protected) you cannot use it in all your classes. The class which you want to be cloned should implement clone method and overwrite it.

How does clone function work?

clone() is a method in the Java programming language 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.


2 Answers

The first thing you should know about Cloneable is - don't use it.

It is very hard to implement cloning with Cloneable right, and the effort is not worth it.

Instead of that use some other options, like apache-commons SerializationUtils (deep-clone) or BeanUtils (shallow-clone), or simply use a copy-constructor.

See here for the views of Josh Bloch about cloning with Cloneable, which explains the many drawbacks of the approach. (Joshua Bloch was a Sun employee, and led the development of numerous Java features.)

like image 92
Bozho Avatar answered Oct 18 '22 20:10

Bozho


Cloneable itself is unfortunately just a marker-interface, that is: it does not define the clone() method.

What is does, is change the behavior of the protected Object.clone() method, which will throw a CloneNotSupportedException for classes that do not implement Cloneable, and perform a member-wise shallow copy for classes that do.

Even if this is the behavior you are looking for, you'll still need to implement your own clone() method in order to make it public.

When implementing your own clone(), the idea is to start with the object create by super.clone(), which is guaranteed to be of the correct class, and then do any additional population of fields in case a shallow copy is not what you want. Calling a constructor from clone() would be problematic as this would break inheritance in case a subclass wants to add its own additional cloneable logic; if it were to call super.clone() it would get an object of the wrong class in this case.

This approach bypasses any logic that may be defined in your constructors though, which could potentially be problematic.

Another problem is that any subclasses that forget to override clone() will automatically inherit the default shallow copy, which is likely not what you want in case of mutable state (which will now be shared between the source and the copy).

Most developers don't use Cloneable for these reasons, and simply implement a copy constructor instead.

For more information and potential pitfalls of Cloneable, I highly recommend the book Effective Java by Joshua Bloch

like image 26
Luke Hutteman Avatar answered Oct 18 '22 19:10

Luke Hutteman