The following code describes how the cloneable works
You can comment the method clone() and run the app again to find what the differences are.
package org.cxz.clone;
import java.util.concurrent.atomic.AtomicInteger;
public class Target implements Cloneable{
public AtomicInteger num = null;
@Override
protected Object clone() throws CloneNotSupportedException {
Target ret = new Target();
ret.num = new AtomicInteger(num.get());
return ret;
}
public static void main(String[] args){
AtomicInteger oldNumber = new AtomicInteger(100);
Target origin = new Target();
origin.num = oldNumber;
Target cloner = null;
try {
cloner = (Target)origin.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cloner.num.addAndGet(100);
System.out.println(origin.num);
System.out.println(cloner.num);
}
}
You can comment the method clone() and run the app again to find what the differences are.