Reputation: 262
public class UserWord extends WordADT {
public int WORD_STATUS;
public int POINT_OF_WORD;
public int COUNT_OF_WRONG_ANSWER;
@Override
public Object getClone() throws CloneNotSupportedException {
return super.clone();
}
}
AND `
Userword temp = new Userword();
Usertword temp2 = temp.getClone(); //this way doesn't work.
I can't use getClone() method. I'm getting this error. How can i clone a instance?
java.lang.CloneNotSupportedException: Class UserWord doesn't implement Cloneable.
Fixed: clone:() method needs to implement IClonable inferface
Upvotes: 0
Views: 587
Reputation: 5011
Use it to clone any object :
public static Object deepClone(Object object) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
return null;
}
}
In your case use like bellow:
Usertword temp2 = (Usertword)deepClone(temp);
Upvotes: 1