3

I was reading this article and it says that

Object's clone method is very tricky. It's based on field copies, and it's "extra-linguistic." It creates an object without calling a constructor".

All I see in the grep code is the following line :

protected native Object clone() throws CloneNotSupportedException;

What am I missing here ?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Inquisitive
  • 7,476
  • 14
  • 50
  • 61

4 Answers4

5

You're missing the native which means it's implemented in non-Java code (in this case it's implemented in the JVM itself).

That's because the exact functionality of clone can not be implemented in Java code (which makes it so problematic).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Why do you say that exact functionality of clone() can not be implemented in Java ? – Inquisitive Jul 05 '12 at 14:53
  • 4
    @Subhra: as the article you linked to indicates, `clone()` creates a new object without ever invoking a constructor. There are only two mechanisms in Java that allow this: `clone()` and serialization. *Both* are implemented in native (non-Java) code. The only way that pure Java code can create objects (without those two mechanisms) is to use `new` (or the equivalent `Class.newInstance()`) and that *always* invokes a constructor. – Joachim Sauer Jul 05 '12 at 14:57
  • +1 thanks for the clarification. But we can also use Reflection to create a new Instance without invoking constructor or am I wrong ? – Inquisitive Jul 05 '12 at 14:58
  • Out of curiosity, if the creators of Java had not used `Cloneable` to decide whether an object should support memberwise cloning (having a native `Clone` method which was callable, but could throw an exception), instead simply defined a "native" class `CloneableObject` with a protected virtual ("native magic") `CloneBase` method, would the resulting framework have been any less expressive? Since `Object.Clone` is only (successfully) callable if the lowest-level class which extends `Object` has decided to implement `Cloneable`, it would seem that having such a class inherit `CloneableObject`... – supercat Jul 05 '12 at 15:20
  • ...would not place any restrictions on the design of such class, but would avoid the need for callers of `Clone` to catch a `nonImplemented` exception. – supercat Jul 05 '12 at 15:22
  • @Subhra To create a new instance with Reflection, you first get a `Constructor` object and then invoke `newInstance` on it. So you still invoke a constructor. – ewan.chalmers Jul 05 '12 at 16:00
  • Basically, the only "sane" replacement for `clone()` is to write a copy constructor or a copy factory method. (Effective Java makes this point better than I could, though.) – Louis Wasserman Jul 05 '12 at 16:16
  • Could the downvoter tell me what's lacking in my answer, please? – Joachim Sauer Jul 05 '12 at 19:10
4

The native keyword indicates that the implementation is in native (non-Java) code.

ewan.chalmers
  • 16,145
  • 43
  • 60
4

First of all, to actually understand the concept behind clone better I recommend the answer to the question: How to properly override clone method?

Regarding the source code you have put into your question:

native means here, that this is a method which is not implemented with Java, but with another language, often C or C++. It's still part of the JVM, hence you can find the actual implementation in the OpenJDK™ Source Release in the

"openjdk/hotspot/src/share/vm/prims/jvm.cpp":539

JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
  JVMWrapper("JVM_Clone");
  Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
  const KlassHandle klass (THREAD, obj->klass());
  JvmtiVMObjectAllocEventCollector oam;
  .
  .
  .
JVM_END
Community
  • 1
  • 1
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
2

The method is marked as native, so you cannot see its implementation because it is not in Java.

adranale
  • 2,835
  • 1
  • 21
  • 39