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 ?

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

8 Comments

Why do you say that exact functionality of clone() can not be implemented in Java ?
@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.
+1 thanks for the clarification. But we can also use Reflection to create a new Instance without invoking constructor or am I wrong ?
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...
...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.
|
4

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

Comments

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 

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.