7

I was reading about the cloning in Java, how to make shallow/deep copies of object etc.

I was wondering why do I need to create object clones in Java? Any real time examples could be helpful in understanding.

Anand
  • 20,708
  • 48
  • 131
  • 198
  • possible duplicate of [clone() method in Java](http://stackoverflow.com/questions/6384826/clone-method-in-java) ... or http://stackoverflow.com/questions/6182565/java-deep-copy-shallow-copy-clone or http://stackoverflow.com/questions/5279256/is-clone-in-java-shallow-copy or ... dozens of others. Please use the search before asking new questions; many have been asked and answered. – Brian Roach Apr 08 '12 at 18:23
  • Hi Brian..I did search it but in most of the posts only the implementation or difference between shallow and deep copies were given. I know about shallow/deep cloning.I just wanted to know the reason to clone an object in Java? – Anand Apr 08 '12 at 18:40

6 Answers6

8

Having a cloned copy of something means you can have "before" and "after" versions. You can leave the original alone while you test something out with a copy. You can provide undo by simply reverting to the original version.

DOK
  • 32,337
  • 7
  • 60
  • 92
  • I understand it. But if you can let me know any real life instances where I have to leave the original object alone and test with the object's copy, it will make the things more clear to me. – Anand Apr 08 '12 at 18:32
  • I think @biziclop has suggested a scenario where you want to test the copy. That is, you want to test that the copy is successful, perhaps in a transaction. If the copy fails to succeed because of validation problems or database transaction failure, you can just discard it and revert to the original without having to recreate it. – DOK Apr 08 '12 at 18:35
  • Yes, I got it. One more thing, I read that we clone an object when initializing an object is too expensive So, instead of creating a new object, we can clone an existing one and only change the properties that matter to our new instance.Could you please throw some light on it? – Anand Apr 08 '12 at 18:46
  • @Anand, did you got your answer, is initializing and object more expensive operation compare to cloning? – Narendra Pandey Jul 04 '21 at 15:43
4

Quite often you want to use immutable objects, in which case cloning is an essential part of your code. If for example you have an immutable object that has a list or array type field, your getter should always return a clone of the list or array to preserve immutability.

The other typical use case is when you want "transactional" modifications, when you call several state changing methods but only want the result to be visible if all of them are successful.

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • As you said, having list as an immutable object's field, I have to return a clone of that list in getter but I can create a new list using 'new' and then return it. Also List interface doesn't have clone method, only implementation classes have clone method.Why should I use clone? – Anand Apr 08 '12 at 18:28
  • @anand I meant cloning as an operation in general. One way of cloning is indeed to use copy constructors. Another is to implement a `clone()` method. It doesn't really matter how you achieve cloning, one way is just as good as the other. – biziclop Apr 08 '12 at 19:04
2

A concrete example of cloning is the: prototype design pattern

1

As Cloning itself says Duplicate copy of something, so In java when we say cloning of object it means to create or have another same object of existing one.

When we do cloning? when we saw that the creating new object every time is time consuming or we need new object having same or little bit difference w.r.t all ready created object, then we use cloning.

Cloning are of 3 types in java

  • Shallow copy
  • Deep copy
  • cloning

Shallow copy

Shallow copy is the process in which the state of the object is copied to another object, but both the objects point to the same reference in heap area.

Deep Copy

In Deep Copy, two separate objects are created and in deep copy. In this each field of one object is copied to another object.

Now third category to overcome this difficulty in java is the concept of cloning.

Cloning in java is done by implements Cloneable interface. Cloneable is marker interface.

For more deep knowledge on cloning Refer : Cloning in java

0

You may use a deep cloned copy of your object because you may need a partial result in some method which you would like to use later.

dexametason
  • 1,133
  • 7
  • 16
0

As a way to help preserve encasulation (and therefore make you code more robust), you could clone objects before returnng them from a getter. For example, a getDate method might clone a date field before returning to the caller.

James Scriven
  • 7,784
  • 1
  • 32
  • 36
  • As you said, I can clone a date field before returning but I can also create a new date object Why should I use clone here? – Anand Apr 08 '12 at 18:30