Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason for ever needing to clone an object in java?

Tags:

java

cloneable

I was reading Joshua Bloch's Effective Java. In there he talks about not using the Clonable interface. I'm a bit of a noob so my question is, whats a use-case for when cloning would be required in code? Could someone give a sticky example so I can grasp the concept?

like image 553
Horse Voice Avatar asked May 14 '14 02:05

Horse Voice


People also ask

Why do we need object cloning in Java?

The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed, so we can use object cloning.

What is object cloning why it is needed?

Object cloning refers to the creation of an exact copy of an object. It creates a new instance of the class of the current object and initializes all its fields with exactly the contents of the corresponding fields of this object. Using Assignment Operator to create a copy of the reference variable.

Why object clone method is protected?

The clone() is protected because it is not declared in the Cloneable interface. Because of this reason, the clone method becomes somewhat useless as it might make the copies of your existing data.

What is meant by clone () method in Java?

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.

What is the use of clone() method in Java?

The clone () method will perform the same task while writing far fewer lines of code. Clone () helps to copy an array quickly. Cloning is the most efficient/easiest way for copying objects. The method clone () is protected, which is defined in Object class. So, we will have to define our own method.

Why object clone is not working in Java?

Object.clone() doesn't invoke any constructor so we don't have any control over object construction. If you want to write a clone method in a child class then all of its superclasses should define the clone() method in them or inherit it from another parent class.

Why do we need to clone an object?

If two reference are pointing to same object then modification to one reference will reflect in another reference as well. To prevent such situation, cloning is required. In cloning, a copy of object is to be created and used to that both objects can be modified independently. This is one of the major advantage of cloning.

How to implement Cloneable interface in Java?

The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone () method generates CloneNotSupportedException . The clone () method is defined in the Object class.


3 Answers

A clone() interface provides a mechanism to create a "shallow" copy of an object. That is, by default, more memory would be allocated for the copy, and each part of the original would be copied into the copy. In contrast, a simple assignment of an object instance to a variable would result in an additional reference to the same object. While the cloned object is itself a true copy, its contained elements are by default references to the ones referred to from the original. If a true "deep" copy is needed, the clone() method would need to be specialized to create clones of its members as well.

One possible use case for a clone() interface would be for implementing a version history of an object to allow a rollback to an older version of it. This could be used in transactional systems, like databases.

Another use case would be for implementing copy-on-write, and this can be useful when the user of the object is only provided a read-only version of the object.

* Description of clone corrected, with thanks and kudos to newacct.

like image 71
jxh Avatar answered Oct 17 '22 18:10

jxh


From Wikipedia on the Prototype pattern,

... you would need to clone() an Object when you want to create another Object at runtime that is a true copy of the Object you are cloning. True copy means all the attributes of the newly created Object should be the same as the Object you are cloning. If you could have instantiated the class by using new instead, you would get an Object with all attributes as their initial values. For example, if you are designing a system for performing bank account transactions, then you would want to make a copy of the Object that holds your account information, perform transactions on it, and then replace the original Object with the modified one. In such cases, you would want to use clone() instead of new.

like image 26
Elliott Frisch Avatar answered Oct 17 '22 18:10

Elliott Frisch


One example is defensive copying of parameters (Effective Java Item 39: Make defensive copies when needed)

class X {
  private byte[] a;

  X(byte[] a) {
      this.a = a.clone();
  }
...
like image 1
Evgeniy Dorofeev Avatar answered Oct 17 '22 17:10

Evgeniy Dorofeev