object cloning with out implementing cloneable interface

六月ゝ 毕业季﹏ 提交于 2019-12-21 03:35:22

问题


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.


回答1:


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.




回答2:


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.




回答3:


Using Reflection API you can achieve it




回答4:


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/




回答5:


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();
}
}



回答6:


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;
}

}




回答7:


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.



来源:https://stackoverflow.com/questions/8192223/object-cloning-with-out-implementing-cloneable-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!