1

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.

Jim
  • 18,826
  • 34
  • 135
  • 254
  • The book should also say that it's not a good idea to use clone().... the problem you've come across is that you must rely on the super class implementing the method properly, otherwise you will get into trouble. To properly clone an Object, you must copy all fields (if the files are primitive, they are obviously immutable so you can simply copy the reference using = instead of clone, as for any immutable Object). Instead of using clone, try a copy constructor or a static factory method. Much better. – Renato May 02 '12 at 06:33
  • 2
    `Object#clone` is implemented as a native method. See http://stackoverflow.com/questions/6825982/how-does-clone-work-under-the-hood – Ismail Badawi May 02 '12 at 06:34

3 Answers3

3

This declaration in Object

  protected native Object clone() throws CloneNotSupportedException;

... means that the clone method 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 implementing Cloneable. But what it does cannot be expressed in plain Java.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    So it is works for primitive types? I.e. does something like `memcopy`? – Jim May 02 '12 at 06:54
  • It is a bit like memcopy. It does a low-level copy all of the fields, and some of the hidden information in the object header. – Stephen C May 02 '12 at 07:32
2

clone() is a method in the Java 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.

AurA
  • 12,135
  • 7
  • 46
  • 63
1

It is right, when you clone an object you should always call super.clone()(as first statement) and then cloning the rest part of your object by hand, this is because when you clone an object you have to make sure that the super class part of the object get clone in the right way too ...

If an object do not support clone it throws CloneNotSupportedException , this means that if you try to clone a super object and you get the exception, you can't implement the clone object also in your subtype, and the object cannot be cloned ... There are several situation when forbid an object cloning operation is right.

aleroot
  • 71,077
  • 30
  • 176
  • 213