to clone the object do i need to implement 'cloneable' interface. because here my class is a jar file(i mean API). so i can't edit the class. i heard that all classes are extends the base object class and this object class implements cloneable interface. does that mean can we directly clone the object with out implementing the interface. if so in my eclipse i am not getting any option to clone the object. is there any other way to clone the object without implementing the cloneable interface. please explain.
-
1Why do you feel you must do this? You may be better off telling us the overall problem that you're trying to solve rather than the programming steps that you feel might be necessary to solve this. In other words, you may be going about things all wrong. – Hovercraft Full Of Eels Nov 19 '11 at 06:09
-
Per comments in the answers, I think the OP wants a copy constructor. – clstrfsck Nov 19 '11 at 06:17
-
hi hovercraft. here i am explaining my issue clearly. i have a class named XYZ and i have a method which returns a object of this XYZ class. private XYZ getObject(){ return obj; } and i created one object by calling this method. XYZ obj1 = getObject(); here comes the problem. actually i want to create one more object with out calling the method again. and i can't edit the XYZ class here. i wanted to do XYZ obj2 = (XYZ)obj1.clone(); but i can not implement the 'cloneable' interface on my XYZ class. so is there any other way to create one more object for this class. – ran Nov 19 '11 at 06:30
-
I think i can rephrase the question as , As we know clone() method already defined in Object Class, And every object is child object of Object class, Then : 1. Why still we need to implement Cloneable interface ? 2. Is there any object that can be cloned without implementing Cloneable interface ? – dinesh kandpal Mar 16 '20 at 18:09
8 Answers
It's usually best practice to avoid clone() anyway because it's difficult to do correctly (http://www.javapractices.com/topic/TopicAction.do?Id=71). Perhaps the class in question has a copy constructor?
Alternatively if it implements Serializable or Externalizable, you can deep copy it by writing it to a byte stream and reading it back in
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
Object deepCopy = ois.readObject();
(from http://www.jguru.com/faq/view.jsp?EID=20435). This is quick and easy but not pretty... I would generally consider it a last resort.

- 634
- 6
- 21
-
hi kyle. thanks for your reply. but i did'nt understand exactly about this. can u please tell me in my case how can i do it. – ran Nov 19 '11 at 06:37
-
A copy constructor would look like: public XYZ(XYZ toCopy). Instead of calling o.clone(), you would call new XYZ(o). The serializable part means that if XYZ can be written to a stream, then you can write it to a stream (actually just an array of bytes in memory) and then read it back from that stream, effectively creating a copy. – kylewm Nov 19 '11 at 06:42
-
The limitation of a copy constructor is that you need to know the class type at code authoring time. `public XYZ(toCopy)` won't work if I pass in a subclass of `XYZ`. – Steve Kuo Nov 30 '11 at 01:09
The Java Object
class does not implements the Cloneable
interface. It does however have the clone()
method. But this method is protected
and will throw CloneNotSupportedException
if called on an object that does not implement the Cloneable
interface. So if you cannot modify the class you want to clone you're out of luck and will have to find another way to copy the instance.
It should be note however that the clone system in Java is full of holes and generally not used anymore. Check out this interview with Josh Bloch from 2002 explaining a few of the issues.

- 397
- 1
- 6
- 21

- 2,130
- 16
- 20
-
is there any other way to clone the object without implementing the cloneable interface...thanks for your reply – ran Nov 19 '11 at 06:08
-
"It should be note however that the clone system in Java is full of holes and generally not used anymore." -- Can you add reference for the statement? – Gaurav Saxena Nov 19 '11 at 06:11
-
You can create a new instance using a constructor and copy the required state across. The feasibility of this really depends upon the class of object you want to copy. – orien Nov 19 '11 at 06:13
-
hi orien. i am exactly stucked at the place where i can not modify the class which i want to clone. can you please suggest me another way to do this cloning. – ran Nov 19 '11 at 06:18
Their is a api which clone the object without implementing the cloneable interface.
Try this
https://github.com/kostaskougios/cloning
Also you can find more details about cloning objects here
http://javatechniques.com/blog/faster-deep-copies-of-java-objects/

- 9,459
- 6
- 61
- 97
It is not mandatory to implement cloneable interface to make a clone of a object. You can write your own clone method in the class whose object you want to clone.

- 81
- 4
You could use Unsafe to create a instance of an object and than copy the values into the new instance by using java reflection. But like the name Unsafe says this is not a really good solution.
public static Unsafe unsafe;
static {
Field f;
try {
f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
unsafe = (Unsafe) f.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static <T> T clone(T object) throws InstantiationException {
T instance = (T) unsafe.allocateInstance(object.getClass());
copyInto(object,instance);
return instance;
}
public static void copyInto(Object source,Object destination){
Class<?> clazz = source.getClass();
while (!clazz.equals(Object.class)) {
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
try {
field.set(destination, field.get(source));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
clazz = clazz.getSuperclass();
}
}

- 358
- 1
- 9
Trying to invoke clone method on a class which doesn't implements Cloneable throws CloneNotSupported Exception and no Object class doesn't implement Cloneable.
here is the javadoc from the clone method of Object class
CloneNotSupportedException if the object's class does not
* support the <code>Cloneable</code> interface. Subclasses
* that override the <code>clone</code> method can also
* throw this exception to indicate that an instance cannot
* be cloned.
Also Object#clone method is protected so you need to implement the clone method in your class and make it public so it can be accessible to classes making objects of your class which can then invoke clone. A good example is the way clone is implement in ArrayList
ArrayList implements cloneable like below public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable
and then implements the clone method:
/**
* Returns a shallow copy of this <tt>ArrayList</tt> instance. (The
* elements themselves are not copied.)
*
* @return a clone of this <tt>ArrayList</tt> instance
*/
public Object clone() {
try {
ArrayList<E> v = (ArrayList<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}

- 4,257
- 3
- 19
- 17
-
hi gaurav, as i already mentioned i can't implement the cloneable interface on my class. but i want to create the duplicate object with different name of the existing object. is there any other way to do this. – ran Nov 19 '11 at 06:10
-
If you are asking if you can clone a object of a class which you cannot edit. The answer is no. Theoretically, you can use reflection to access the variables of the class and thus be able to get and replicate its state, but it depends on the Security manager if you are allowed to access the private members of the class. Again even in that case, deep copying may not be possible. – Gaurav Saxena Nov 19 '11 at 06:16
-
You should probably use a copy constructor in this case rather than `clone()`. First link in google is this one: http://www.javapractices.com/topic/TopicAction.do?Id=12 – clstrfsck Nov 19 '11 at 06:16
The clone() method in Object class is protected which means all classes will inherit it with protected access modifier, hence if you try to access it outside that class without cloning it you wont see it, also it will throw you CloneNotSupportedException if you try to invoke it without implementing Cloneable interface.
In case you are looking at having a way create a behaviour of clone you need to write a new method in your class and then you create a copy of all the fields in that, its basically like create a new copy of an existing state of an object.
public class TestCloneable {
private String name = null;
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
public TestCloneable createCopy(){
TestCloneable testCloneable = new TestCloneable();
testCloneable.setName(this.getName());
return testCloneable;
}
}

- 20,107
- 7
- 46
- 63