ITeye博客

an example of cloneable

Aga · 2009-10-10 09:43 · J2SE · Markdown

The following code describes how the cloneable works

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.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。