aspose file tools*
The moose likes Java in General and the fly likes Shallow Cloning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Shallow Cloning" Watch "Shallow Cloning" New topic
Author

Shallow Cloning

Shivani Yadav
Greenhorn

Joined: Aug 19, 2009
Posts: 6
I am having some confusion in understanding shallow clonning. Just want to clear my understanding.

As per shallow clonning ..."In shallow copy the object is copied without its contained objects.
Shallow clone only copies the top level structure of the object not the lower levels..."

I just tried the below code to see practically.



In the above code i am having a query.
When i am doing " clonedObj.str1 = "String33"; " then why the origObj.str1 value is not changed? i.e. origObj.str1 is "String1" and not the "String33". Here are they not referring to the same String refrence?

This is not happening when i am doing "clonedObj.fsh.setName("Octopus");" i.e origObj.fsh name also gets modified as they are referring to the same Fish object.

Can anybody please explain me the difference between above two scenarios.

Thanks in advance.

♫ The deepest secret...life is not a process of discovery, but a process of creation. Seek therefore, not to find out Who You Are, but seek to determine Who You Want to Be
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

In first case, you are changing the "Fish" object's name, but both "Shallow" objects refer to the same "Fish" object. But with "str1" reference though both objects initially refers to the same, changing one's reference doesn't change the other's (that is, Java uses "pass by value" so str1 of the object you changed will refer to the new String object ).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Shivani Yadav
Greenhorn

Joined: Aug 19, 2009
Posts: 6

Thanks, i got it.

When i am setting the name of the Fish object, i am not changing the object to which clonedObj.fsh and origObj.fsh is referring to. Only the object's state is changed.

But when i am assigning value to clonedObj.str1 the actual object to which it was referring is changed so i will see the difference.
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110

So while cloning a separate shallow object is created but the same fish object is used by both the shallow objects(the clone one and the parent).Why is that so?


To err is human,
To forgive is not company policy
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

PrasannaKumar Sathiyanantham wrote:So while cloning a separate shallow object is created but the same fish object is used by both the shallow objects(the clone one and the parent).Why is that so?

That's how shallow copy works (clone() in Object class), It only copies object references. If you want deep copy (it's own copies for the cloned object) you should override the clone() in your class.
Jack Bento
Ranch Hand

Joined: Jun 14, 2009
Posts: 34
Is this in the exam?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Fray Bentos wrote:Is this in the exam?

No, nothing about cloning on the SCJP exam. Check the Objectives for SCJP 6.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Agreed, interesting topic, not on the exam.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shallow Cloning