Inquisitive
Inquisitive

Reputation: 7856

clone () implementation in Object class

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 ?

Upvotes: 3

Views: 508

Answers (4)

Konrad Reiche
Konrad Reiche

Reputation: 29553

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

Upvotes: 4

adranale
adranale

Reputation: 2874

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

Upvotes: 2

Joachim Sauer
Joachim Sauer

Reputation: 308269

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).

Upvotes: 5

ewan.chalmers
ewan.chalmers

Reputation: 16265

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

Upvotes: 4

Related Questions