Why do we use the clone()
method in Java? (Please give the answer in respect of memory constraint.) Will that reduce memory usage? If yes, then how? Will that reduce the effect of memory leak?
-
We don't. I've never used it in 18 years, or a 'copy constructor' either. – user207421 Nov 14 '15 at 07:32
-
take a deep dive here http://howtodoinjava.com/core-java/cloning/a-guide-to-object-cloning-in-java/comment-page-1/#comment-25762 – Bharatesh Jul 07 '17 at 10:29
7 Answers
Apart from do not use clone, implement a copy constructor, you asked about memory constraints.
The idea of cloning is to create an exact duplicate of the cloned object. So in worst case, you use twice the amount of memory afterwards. Practically - a bit less, because Strings are often interned and will (usually) not be cloned. Even though it's up to the implementor of the clone method/copy constructor.
Here's a short example of a class with a copy constructor:
public class Sheep {
private String name;
private Fur fur;
private Eye[2] eyes;
//...
// the copy constructor
public Sheep(Sheep sheep) {
// String already has a copy constructor ;)
this.name = new String(sheep.name);
// assuming Fur and Eye have copy constructors, necessary for proper cloning
this.fur = new Fur(sheep.fur);
this.eyes = new Eye[2];
for (int i = 0; i < 2; i++)
eyes[i] = new Eye(sheep.eyes[i]);
}
}
Usage:
Sheep dolly = getDolly(); // some magic to get a sheep
Sheep dollyClone = new Sheep(dolly);

- 113,398
- 19
- 180
- 268
-
1What if Sheep extended some base class, say `Animal` which had attributes that needed to be called as well. Would the copy constructor approach imply that *both* `Animal` and `Sheep` have copy constructors, and the `Sheep` CC would have to call `super(sheep)`? – Adam Parkin Feb 21 '13 at 17:49
-
1Yes. The call would be `super(sheep);` and the superclass would init its fields based on that instance. You need to call the (one) constructor of the superclass anyway. – Andreas Dolk Feb 22 '13 at 11:37
We should not use it. It is a broken and obsolete idiom, which should be avoided in new code. Better use a copy constructor instead whenever you can.

- 1
- 1

- 114,404
- 31
- 268
- 329
The clone() copies the values of an object to another. clone() method saves the extra processing task for creating the exact copy of an object.
As you can see in the below example, both reference variables have the same value.
class Student18 implements Cloneable {
int rollno;
String name;
Student18(int rollno, String name) {
this.rollno = rollno;
this.name = name;
}
public static void main(String args[]) {
try {
Student18 s1 = new Student18(101, "amit");
Student18 s2 = (Student18) s1.clone();
System.out.println(s1.rollno + " " + s1.name);
System.out.println(s2.rollno + " " + s2.name);
} catch (CloneNotSupportedException c) {
}
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Output :
101 amit
101 amit
If we create another object by new keyword and assign the values of another object to this one, it will require a lot of processing on this object. So to save the extra processing task we use clone() method.

- 57
- 9

- 4,559
- 3
- 32
- 35
if we need to use many object having the same data, then don't create objects using new keyword. use clone method to create that object, because operation of creating object with clone method is faster than using new keyword.

- 81
- 4
Making a copy of an object seems at first to be a straight forward task:
Simply copy the values of all the properties into another instance of the same class. But what about the variables that are references to other objects? Copies of these reference values mean they will point to the same objects as the first class.
But maybe that is not what we want. Perhaps we want all the objects referenced by the copy to be independent copies as well.
These two types of object copies are called:
shallow copy - exact bit copy of all the attributes of the original object deep copy - primitives are copied exactly but objects referenced are copied rather than the references themselves. The Object class, which is inherited by all Java classes, includes the clone() method that will make exact bit copies of all the properties.
However, clone() is a protected method. So a given object can not be cloned by instances of any classes outside the package (unless they are subclasses of that object's class). This allows the class designer to specify explicitly what kind of clones (shallow or deep) to make.
Java requires classes that want to override the clone() method, to implement the cloneable interface. The clone() method must be made public as well so as to override the access restrictions.
For example, the HashTable class implements cloneable. Its clone() method makes a shallow copy so the keys and values of the copied HashTable will reference the same objects as the original.
Many core Java classes, however, do not implement cloneable. If the clone() method is invoked for such classes, a CloneNotSupportedException will result.

- 528
- 1
- 5
- 12
in few words it is used to copy the objects instead the references, it increase the memory usage.

- 552
- 2
- 8
- 17
-
That's some weird advice. Clone shouldn't be any more difficult to implement than any other method of deep copying an object. The only difference is that `clone` will start you off with a shallow copy, which might do some or all of the work needed if all your fields are primitives and/or immutable objects. The advice to implement `clone` in a new class whose parents implement `clone` with an exception with no discussion is just wrong. For example, if you pass your new class into a method that uses `clone` on a base class, your code will just fail. – user904963 Feb 06 '22 at 17:11