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
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 ).