---
title: "an example of cloneable"
author: "Aga"
date: "2009-10-10T09:43:47Z"
tags: []
slug: "an-example-of-cloneable"
draft: false
lastmod: "2009-10-10T09:43:47Z"
id: 484134
url: "https://aga.iteye.com/blog/484134"
---

The following code describes how the cloneable works

```java
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.