I have a class SBContainer which has a StringBuffer member mySB. I am implementing Cloneable for SBContainer like this –
SBContainer implements Cloneable {
public StringBuffer mySB;
public SBContainer() {
mySB = new StringBuffer("This is a test string");
}
public Object clone() throws CloneNotSupportedException {
SBContainer cloned = (SBContainer)super.clone();
return cloned;
}
}
Now, I create an object sbc1 for MyContainer and it’s clone sbc2. It looks like the container mySB is not cloned; and sbc1.mySB and sbc2.mySB are pointing to the same StringBuffer object. I tested using the following class –
public class SBContainerTest {
public static void main(String[] args) {
SBContainer sbc1 = new SBContainer();
SBContainer sbc2 = null;
try {
sbc2 = (SBContainer)sbc1.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace();
}
sbc1.mySB.append(" ...something appended");
System.out.println(sbc2.mySB);
}
}
EDIT:
The output was: This is a test string ...something appended
So, I tried to clone mySB like this –
cloned.mySB = (StringBuffer)mySB.clone();
…and I get this error –
SBContainerTest.java:8: clone() has protected access in java.lang.Object
cloned.mySB = (StringBuffer)mySB.clone();
^
1 error
So, how can I achieve this? How can I clone a mutable member of a class which implements Cloneable?
Thanks.
StringBufferis notCloneable, so you’ll have to clone it manually. I suggest something like this in yourclonemethod: