I wanted to know how Java does Object Cloning. But when I read the Object.java class I saw that it was not defined in it. Im confused. I really dont know who internals of java are implemented. It would be nice if someone could help me with this.....

Recommended Answers

All 10 Replies

protected native Object clone() throws CloneNotSupportedException;

Notice the native keyword. This means that the method is not implemented using Java, but using native code (C/C++) using the Java Native Interface (http://java.sun.com/docs/books/jni/).

What doc are you getting that from?
My API doc has this:
protected Object clone()

The API doc does say protected Object clone() yes. The source code, shows it with the native keyword.

How many other methods are native? I'll bet a lot.

What doc are you getting that from?
My API doc has this:
protected Object clone()

Just open the class in IDE and get source (that is how it does work in IntelliJ), then you can see internal structure :)

Adding on to others, it also pays to know that cloning in Java is pretty much broken and for practical purposes you are better off using other alternatives (hand rolled copy constructor).

OH Thankyou. What if I wanted to see the native code for Cloning?

There's a load of non-Java-language stuff in the source code download, including C code. I haven't looked at that, but you may find it there?

Oh Thank you. My first guess would be that it would make a memory copy. But Ill try to lookup the source code to see if I can find stuff..

Thanks DW team!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.