Animal animal = new Animal(101); //Constructor is executed.
Animal clone=(Animal)animal.clone() //Constructor is not executed. Why ?

- 62,134
- 8
- 100
- 147

- 162
- 11
-
4The Java specification says it so. I recommend Joshua Bloch's Effective Java book, for more info. – Nagy Vilmos Mar 18 '15 at 13:53
-
Cloning does not call the constructor for you. If you want to run a constructor, call a constructor explicitly. – Sergey Kalinichenko Mar 18 '15 at 13:56
-
Why do you think it should call constructor? – Rohit Jain Mar 18 '15 at 13:57
-
A clone is not created using a constructor. Simply creates new instance and copies the values of the fields. (unless you override the clone method to do it in another way) – Bubletan Mar 18 '15 at 13:57
-
1@Sergey Pauk Yes it does. – Bubletan Mar 18 '15 at 14:01
-
@Sergey Pauk Implement `Cloneable` and `CloneNotSupportedException` won't be thrown. – Bubletan Mar 18 '15 at 14:04
-
Well I was thinking cloning also creates an object so Should call constructor.@RohitJain – Javed Solkar Mar 18 '15 at 14:06
-
New instance is created without calling constructor ? @Bubletan Thanks in advance. – Javed Solkar Mar 18 '15 at 14:17
-
1@SergeyPauk's comment is incorrect and bad advice. Never call the object's constructor from a `clone` method. The standard way to implement `clone` is http://stackoverflow.com/questions/1052340/what-is-wrong-with-this-clone/1053227#1053227 – Steve Kuo Mar 18 '15 at 15:34
2 Answers
The default implementation of the clone()
method given in the Object
class does not call any kind of constructor.
It makes a "shallow copy" of object, because it creates copy of Object by creating new instance and then copying content by assignment, which means if your Class contains a mutable field, then both original object and clone will refer to same internal object.
Try to have a look at this page.

- 7,971
- 5
- 57
- 106
Your constructor isn't called because you are calling the clone() method. Based on the way how the clone method is implemented, it does not need to call the constructor.
One way to involve the constructor would be by implementing the clone method with a copy constructor, like so:
public class A implements Cloneable{
private int example;
public A(){
//this is the default constructor, normally not visible and normally not needed,
//but needed in this case because you supply another constructor
}
public A(A other){
this.example = other.example;
}
//You may specify a stronger type as return type
public A clone(){
return new A(this);
}
}
Now, every time you call the clone method, the copy constructor (the one with the A-parameter) is called.
regards me

- 609
- 8
- 22
-
1Your class must implement `Clonable`, or a `CloneNotSupported` will be thrown. – riccardo.cardin Mar 18 '15 at 14:27
-
Yeah sorry, I forgot it because I did not write it in an IDE;) I added it now and upvoted your comment ;) – Christian Mar 18 '15 at 14:33
-
I think you should modify your answer, because your example is not correct. Take a look at [this answer](http://stackoverflow.com/questions/1052340/what-is-wrong-with-this-clone/1053227#1053227). – riccardo.cardin Mar 18 '15 at 15:58
-
1Do not call the constructor, rather call `super.clone()`. See http://stackoverflow.com/questions/1052340/what-is-wrong-with-this-clone/1053227#1053227 – Steve Kuo Mar 18 '15 at 16:05