// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class DeepCloneEx {
   public static void main(String[] args) throws Exception {
      MySpecialObj x1 = new MySpecialObj(1, "A");
      MySpecialObj x2 = (MySpecialObj)x1.clone();
      x2.add(2, "B");
      System.out.println(x1);
      System.out.println(x2);
   }
}

class MySpecialObj implements Cloneable {
   private int i;
   private StringBuffer sb;
   public MySpecialObj() {
   }
   public MySpecialObj(int i, String s) {
      this.i = i;
      this.sb = new StringBuffer(s);
   }
   public String toString() {
      return (i + " " + sb);
   }
   public void add(int i, String s) {
      this.i += i;
      this.sb.append(s);
   }
   public Object clone() throws CloneNotSupportedException {
      return new MySpecialObj(i, sb.toString());
   }
}
