GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — java-basics/core-java/methods/DeepCopy.java at master · learning-zone/java-basics Expand file tree/ Copy pathDeepCopy.javaMore file actions 41 lines (32 loc) · 1.01 KB / Copy pathDeepCopy.javaFile metadata and controls41 lines (32 loc) · 1.01 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package methods; /** * In the deep copy, we create a clone which is independent of original object and making changes in the cloned object should not affect original object. * So for deep copy we need to ensure all the member class also implement the Cloneable interface and override the clone() method of the object class. */ import java.util.ArrayList; class DeepCopy1 { int x, y; } class DeepCopy2 implements Cloneable { int a, b; DeepCopy1 c = new DeepCopy1(); public Object clone() throws CloneNotSupportedException { DeepCopy2 t = (DeepCopy2)super.clone(); t.c = new DeepCopy1(); return t; } } public class DeepCopy { public static void main(String[] args) throws CloneNotSupportedException { DeepCopy2 t1 = new DeepCopy2(); t1.a = 10; t1.b = 20; t1.c.x = 30; t1.c.y = 40; DeepCopy2 t3 = (DeepCopy2)t1.clone(); t3.a = 100; t3.c.x = 300; System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " +t1.c.y); System.out.println(t3.a + " " + t3.b + " " + t3.c.x + " " + t3.c.y); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |