Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: What goes wrong with cloning here Date: Fri, 23 Oct 2015 17:49:58 +0200 Organization: Decebal Computing Lines: 149 Message-ID: <87oafplh3t.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="6098"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ei9+jt423EGfjN7fsYniciJvG7tJ2SYc=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:NoWSDFmIf+OoZERxSWQEiUeiV7s= sha1:eCaDf/9kVnAQyUbuYpF40hXe2aE= Xref: csiph.com comp.lang.java.programmer:32931 --=-=-= Content-Type: text/plain I need to use Cloneable. (I know that some people say you should never use it, but it is a requirement of the project.) I made something I thought should work, but it does not. I have attached my dumbed down code. What am I doing wrong? When I in class A change a field from class B in the clone, the field in the original is also changed. That should not happen. --=-=-= Content-Type: text/x-java-source Content-Disposition: attachment; filename=A.java public class A implements Cloneable { private B key; private String c; private String d; public A() { } public Object clone() throws CloneNotSupportedException { A clone = (A) super.clone(); B newKey = new B(); B oldKey = getKey(); newKey.setA(oldKey.getA()); newKey.setB(oldKey.getB()); newKey.setC(oldKey.getC()); newKey.setD(oldKey.getD()); clone.setKey(key); return clone; } public String getC() { return c; } public String getD() { return d; } public B getKey() { return key; } public void setC(String c) { this.c = c; } public void setD(String d) { this.d = d; } public void setKey(B key) { this.key = key; } public static void main(String[] args) throws CloneNotSupportedException { A clone, original; B key; System.out.println("Got here"); original = new A(); key = new B(); key.setA("Some text"); key.setB(12); key.setC(73); original.setKey(key); clone = (A) original.clone(); clone.getKey().setB(99); System.out.println(original.getKey()); System.out.println(clone.getKey()); } } --=-=-= Content-Type: text/x-java-source Content-Disposition: attachment; filename=B.java import java.io.Serializable; import java.util.Date; public class B implements Serializable { private static final long serialVersionUID = 1L; private String a; private int b; private int c; private Date d; public B() { } public String getA() { return a; } public int getB() { return b; } public int getC() { return c; } public Date getD() { return d; } public void setA(String a) { this.a = a; } public void setB(int b) { this.b = b; } public void setC(int c) { this.c = c; } public void setD(Date d) { this.d = d; } public String toString() { return String.format("B [a: %s, b: %s, c: %s, d: %s]", a, b, c, d); } } --=-=-= Content-Type: text/plain -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof --=-=-=--