I am reading about clone from Effective Java
It says that in clone the first method to be called must be super.clone()
In this case, I guess that eventually we would end up calling the clone of java.lang.Object going up the hierarchy chain.
But I thought that object’s clone doesn’t do anything.
Looking in code I see:
protected native Object clone() throws CloneNotSupportedException; and no implementation.
But from the paragraph it seems that if a class has only primitive fields calling
(ClassX) super.clone() is sufficient for creating a clone.
But how? super.clone is of Object.
I am reading about clone from Effective Java It says that in clone the
Share
This declaration in Object
… means that the
clonemethod is implemented in native code; i.e. there is magic going on behind the curtain.You can be assured that
Object.clone()actually does do something … provided that you have declared your class as implementingCloneable. But what it does cannot be expressed in plain Java.