<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Java Clone, Shallow Copy and Deep Copy</title>
	<atom:link href="http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/feed/" rel="self" type="application/rss+xml" />
	<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/</link>
	<description>Blog on core java, servlets, jsp and design patterns.</description>
	<lastBuildDate>Mon, 04 Jun 2012 02:04:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Madhav</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/#comment-12441</link>
		<dc:creator>Madhav</dc:creator>
		<pubDate>Sat, 02 Jun 2012 02:55:21 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-12441</guid>
		<description>Hi Joe 
whatever Anonymous tells is valid point , even I am also getting confused to understarnd differences between deep and shallow clones , Please still more clear
in memory locations perspective,
but whaterver disadvantages explained are very good</description>
		<content:encoded><![CDATA[<p>Hi Joe<br />
whatever Anonymous tells is valid point , even I am also getting confused to understarnd differences between deep and shallow clones , Please still more clear<br />
in memory locations perspective,<br />
but whaterver disadvantages explained are very good</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/#comment-12150</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 23 May 2012 12:46:47 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-12150</guid>
		<description>Hi Joe,

According to you and javadoc Object class clone method provides a shallow copy. 

Shallow copy is the copy which shares the same memory location. but in case of clone java creates a new object(seperate memory location) and copies all the properties of the actual object to new object. I am really very confused. 

http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone%28%29

if you see this link it clearly says that clone creates shallow copy and a new object with copied properties

but if you see this link

http://en.wikipedia.org/wiki/Object_copy

it says that shallow copy shares same memory location and change in one object can change state of copied object

but when i used cloned object and changed value of actual object it did not had any impact on cloned object

Please help...</description>
		<content:encoded><![CDATA[<p>Hi Joe,</p>
<p>According to you and javadoc Object class clone method provides a shallow copy. </p>
<p>Shallow copy is the copy which shares the same memory location. but in case of clone java creates a new object(seperate memory location) and copies all the properties of the actual object to new object. I am really very confused. </p>
<p><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone%28%29" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone%28%29</a></p>
<p>if you see this link it clearly says that clone creates shallow copy and a new object with copied properties</p>
<p>but if you see this link</p>
<p><a href="http://en.wikipedia.org/wiki/Object_copy" rel="nofollow">http://en.wikipedia.org/wiki/Object_copy</a></p>
<p>it says that shallow copy shares same memory location and change in one object can change state of copied object</p>
<p>but when i used cloned object and changed value of actual object it did not had any impact on cloned object</p>
<p>Please help&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/#comment-11669</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 01 May 2012 11:58:06 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-11669</guid>
		<description>Deep clone : You will have to override the clone method in each of the classes that the class to be cloned is composed of.
The below example is just to illustrate the deep clone.

package clone;

public class Customer implements Cloneable{

	String name;
	Order order;
	public Customer(String name, Order order) {
		super();
		this.name = name;
		this.order = order;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Customer() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Customer(String name) {
		super();
		this.name = name;
	}
	
	@Override
	public Object clone(){
		try {
			Customer clonedCustomer = (Customer)super.clone();
			clonedCustomer.setOrder((Order)order.clone());
			return clonedCustomer;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}

package clone;

public class Order implements Cloneable {

	Item item;

	public Order(Item item) {
		super();
		this.item = item;
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}
	
	@Override
	public Object clone(){
		try {
			Order clonedOrder = (Order)super.clone();
			clonedOrder.setItem((Item)item.clone());
			return clonedOrder;
			
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}

package clone;

public class Item implements Cloneable {

	String itemName;

	public Item(String itemName) {
		super();
		this.itemName = itemName;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}
	@Override
	public Object clone(){
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}
}

package clone;

public class ShallowCloneTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Customer cust = new Customer(&quot;Cust001&quot;, new Order(new Item(&quot;Item001&quot;)));
		System.out.println(&quot;original object :&quot; + cust);
		Customer cloned = (Customer)cust.clone();
		System.out.println(&quot;cloned object :&quot; + cloned);
		cloned.getOrder().getItem().setItemName(&quot;Item002&quot;);
		cloned.setName(&quot;Cust002&quot;);
		System.out.println(&quot;original object :&quot; + cust.getOrder().getItem().getItemName());
		System.out.println(&quot;original object :&quot;+ cust.getName());
	}

}</description>
		<content:encoded><![CDATA[<p>Deep clone : You will have to override the clone method in each of the classes that the class to be cloned is composed of.<br />
The below example is just to illustrate the deep clone.</p>
<p>package clone;</p>
<p>public class Customer implements Cloneable{</p>
<p>	String name;<br />
	Order order;<br />
	public Customer(String name, Order order) {<br />
		super();<br />
		this.name = name;<br />
		this.order = order;<br />
	}</p>
<p>	public Order getOrder() {<br />
		return order;<br />
	}</p>
<p>	public void setOrder(Order order) {<br />
		this.order = order;<br />
	}</p>
<p>	public String getName() {<br />
		return name;<br />
	}</p>
<p>	public void setName(String name) {<br />
		this.name = name;<br />
	}</p>
<p>	public Customer() {<br />
		super();<br />
		// TODO Auto-generated constructor stub<br />
	}</p>
<p>	public Customer(String name) {<br />
		super();<br />
		this.name = name;<br />
	}</p>
<p>	@Override<br />
	public Object clone(){<br />
		try {<br />
			Customer clonedCustomer = (Customer)super.clone();<br />
			clonedCustomer.setOrder((Order)order.clone());<br />
			return clonedCustomer;<br />
		} catch (CloneNotSupportedException e) {<br />
			e.printStackTrace();<br />
		}<br />
		return null;<br />
	}</p>
<p>}</p>
<p>package clone;</p>
<p>public class Order implements Cloneable {</p>
<p>	Item item;</p>
<p>	public Order(Item item) {<br />
		super();<br />
		this.item = item;<br />
	}</p>
<p>	public Item getItem() {<br />
		return item;<br />
	}</p>
<p>	public void setItem(Item item) {<br />
		this.item = item;<br />
	}</p>
<p>	@Override<br />
	public Object clone(){<br />
		try {<br />
			Order clonedOrder = (Order)super.clone();<br />
			clonedOrder.setItem((Item)item.clone());<br />
			return clonedOrder;</p>
<p>		} catch (CloneNotSupportedException e) {<br />
			e.printStackTrace();<br />
		}<br />
		return null;<br />
	}</p>
<p>}</p>
<p>package clone;</p>
<p>public class Item implements Cloneable {</p>
<p>	String itemName;</p>
<p>	public Item(String itemName) {<br />
		super();<br />
		this.itemName = itemName;<br />
	}</p>
<p>	public String getItemName() {<br />
		return itemName;<br />
	}</p>
<p>	public void setItemName(String itemName) {<br />
		this.itemName = itemName;<br />
	}<br />
	@Override<br />
	public Object clone(){<br />
		try {<br />
			return super.clone();<br />
		} catch (CloneNotSupportedException e) {<br />
			e.printStackTrace();<br />
		}<br />
		return null;<br />
	}<br />
}</p>
<p>package clone;</p>
<p>public class ShallowCloneTest {</p>
<p>	/**<br />
	 * @param args<br />
	 */<br />
	public static void main(String[] args) {<br />
		Customer cust = new Customer(&#8220;Cust001&#8243;, new Order(new Item(&#8220;Item001&#8243;)));<br />
		System.out.println(&#8220;original object :&#8221; + cust);<br />
		Customer cloned = (Customer)cust.clone();<br />
		System.out.println(&#8220;cloned object :&#8221; + cloned);<br />
		cloned.getOrder().getItem().setItemName(&#8220;Item002&#8243;);<br />
		cloned.setName(&#8220;Cust002&#8243;);<br />
		System.out.println(&#8220;original object :&#8221; + cust.getOrder().getItem().getItemName());<br />
		System.out.println(&#8220;original object :&#8221;+ cust.getName());<br />
	}</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: manu</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/#comment-11635</link>
		<dc:creator>manu</dc:creator>
		<pubDate>Mon, 30 Apr 2012 07:11:28 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-11635</guid>
		<description>hi please give one example of deep copy...
it would be better</description>
		<content:encoded><![CDATA[<p>hi please give one example of deep copy&#8230;<br />
it would be better</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: manoj</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/#comment-11634</link>
		<dc:creator>manoj</dc:creator>
		<pubDate>Mon, 30 Apr 2012 07:10:05 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-11634</guid>
		<description>Really its great topic ....
U explained nicely.... thanks a lot.
Thanks
manoj CITE</description>
		<content:encoded><![CDATA[<p>Really its great topic &#8230;.<br />
U explained nicely&#8230;. thanks a lot.<br />
Thanks<br />
manoj CITE</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous_2</title>
		<link>http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/#comment-11433</link>
		<dc:creator>Anonymous_2</dc:creator>
		<pubDate>Sat, 21 Apr 2012 12:07:23 +0000</pubDate>
		<guid isPermaLink="false">http://javapapers.com/?p=191#comment-11433</guid>
		<description>I dont understand the difference of shallow and deep copy. If we copy your code and implement the deep copy clone then output will be the same. We need to add one object reference to the Employee object then we can understand the difference.</description>
		<content:encoded><![CDATA[<p>I dont understand the difference of shallow and deep copy. If we copy your code and implement the deep copy clone then output will be the same. We need to add one object reference to the Employee object then we can understand the difference.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

