Frage

I was looking for tutorials online about java cloning, but only found the disadvantages to clone() and nothing about the advantages. I would like to know some of the advantages of using Java clone().

War es hilfreich?

Lösung

Cloning has its uses certainly. Imagine any kind of a business application where you have records of data, represented in objects, that you can "save as" (duplicate and rename). If that data is held in a object that implements the Cloneable interface then you can clone the original and update it with the new information.

This is superior to creating a new object instance and copying all of the data over explicitly. Some people address this with helper classes and methods which do the copying, but then you have the information required to copy a class outside of the class itself which is poor OO programming.

Another use case I like is when I use a class to as a backing store for a GUI and that GUI has a reset button. When the GUI is initialized, I clone the backing store object. Then if the user presses restore, I just reinitialize the GUI to the values in the clone object, rather than figure what information they might have changed or get a new copy of the original information from storage. There are many uses, certainly.

But as you know cloning can raise issues in an inheritance framework and clutters up otherwise lightweight data classes, so I wouldn't make an object cloneable unless there was a business requirement for it.

Andere Tipps

I think the reason you're having trouble finding anything about the advantages of cloning is that the advantage is implicit: having a (supposedly) easy way to create an exact duplicate of the original object.

But unfortunately, Java's built-in cloning mechanism via clone() and Cloneable has too many disadvantages to consider , mostly relating to the horrible implications it has for the design of your classes. Josh Bloch has an entire section on why you shouldn't use clone() or Cloneable in his book Effective Java, briefly summed up in an interview.

The general recommendation is to use a copy constructor instead of using clone() or Cloneable, although you still have to decide whether a shallow or deep copy is appropriate. You can also use a serialization API such as Serializable or JAXB, as well as various cloning libraries, to create a deep clone. You can find a very nice discussion in a related question, Java: recommended solution for deep cloning/copying an instance.

Object.clone() have many design issues but it is still the popular and easiest way of copying objects. Some advantages using clone() are

  • It is the easiest way to implement cloning even if for age-old projects, you just need to define a parent class, implement Cloneable in it, provide the definition of clone() method and you are ready every child of your parent will get the cloning feature.
  • Cloning requires very less line of code, just an abstract class with 4 or 5 line long clone() method if you don't require deep copy.
  • Cloning is the fastest way to copy arrays.
  • As of release 1.5, calling clone on an array returns an array whose compile-time type is the same as that of the array being cloned which clearly means calling clone on arrays do not require type casting.

You can read more on Java Cloning - Copy Constructor versus Cloning

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top