tag:blogger.com,1999:blog-8712770457197348465.post3588347165618158140..comments2024-03-17T23:01:26.872-07:00Comments on Javarevisited: How clone method works in Java? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-8712770457197348465.post-88738826604387008122019-05-19T19:07:15.054-07:002019-05-19T19:07:15.054-07:00Thank you @Unknown for taking initiative and expla...Thank you @Unknown for taking initiative and explaining to fellow reader. Much appreciated and yes, you are absolutely correct. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85625280871452473422019-05-19T11:05:03.008-07:002019-05-19T11:05:03.008-07:00ansu, I had the same question as your after readin...ansu, I had the same question as your after reading this article, but as Paul pointed out, the shadow copy is actually having all fields pointing to the same reference in heap; given that it is safe to point to the same reference for immutable/primitive objects, shadow copy is safe enough if the class only contains immutable/primitive objects.<br /><br />The second part of this series elaborates Anonymoushttps://www.blogger.com/profile/08141378684877645703noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82234026622107452372017-10-07T16:09:14.807-07:002017-10-07T16:09:14.807-07:00Hello ansu, in case of Immutable object, multiple ...Hello ansu, in case of Immutable object, multiple reference can point to same object, that's the whole purpose of making it object immutable. Since it cannot be changed, its safe to share. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14417026671819269012017-10-07T12:33:24.312-07:002017-10-07T12:33:24.312-07:00Very nice article, just one correction, you said t...Very nice article, just one correction, you said the clone method will work perfectly in case of immutable objects and primitive but in the 'Shallow copy' image, you have used String class which is immutable and pointing to same reference and that is not correct.Ansuhttps://www.blogger.com/profile/08912595433416173774noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58517625497884058102017-06-27T06:44:10.904-07:002017-06-27T06:44:10.904-07:00@Adrian, clone is used to create copy of an object...@Adrian, clone is used to create copy of an object e.g. if you have a Date you can create another Date object by using clone, which has same attributes or state as previous Date object. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24214187016533304222017-06-26T05:16:25.153-07:002017-06-26T05:16:25.153-07:00Hi, can you please tell me what is the user of a c...Hi, can you please tell me what is the user of a clone method ?<br />Is an instance of a cloned object?Anonymoushttps://www.blogger.com/profile/04948819233000799875noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-23067265136694328332015-11-18T03:41:29.014-08:002015-11-18T03:41:29.014-08:00If equals() method returns true the the hashcode()...If equals() method returns true the the hashcode() must be equal,but reverse may or may not be equal.This is the only contract.Anonymoushttps://www.blogger.com/profile/17752617618541957560noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4615617718356903412015-10-08T01:09:13.762-07:002015-10-08T01:09:13.762-07:00Point 3 :
"This can be dangerous because any...Point 3 : <br />"This can be dangerous because any change made on that mutable field will reflect in both original and copy object.<br />"<br />We can make changes if an object is Mutable.Anonymoushttps://www.blogger.com/profile/02539202225806284218noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-75722860998292019532015-09-30T05:36:12.506-07:002015-09-30T05:36:12.506-07:00@Uday, yes it's an example of shallow cloning,...@Uday, yes it's an example of shallow cloning, but given the object is Immutable it didn't really matter. See the <a href="http://javarevisited.blogspot.sg/2014/03/how-to-clone-collection-in-java-deep-copy-vs-shallow.html" rel="nofollow">second part</a> of this article for cloning an object with the mutable field, which involves deep copy. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54523731352910353992015-09-30T02:13:17.174-07:002015-09-30T02:13:17.174-07:00This here what listed is Example of Shallow copy r...This here what listed is Example of Shallow copy right ???udayhttps://www.blogger.com/profile/13108582718881204870noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26778278229750356032015-05-15T09:32:13.362-07:002015-05-15T09:32:13.362-07:00A Cloneable interface which gives permission to cl...A Cloneable interface which gives permission to clone() method in a given program which throws CloneNotSupportedException.U can check it more clearly in this video.https://youtu.be/kzOrWHgRe4AAnonymoushttps://www.blogger.com/profile/04017134156660040240noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-5640966829839866972015-01-03T08:51:49.036-08:002015-01-03T08:51:49.036-08:00"This information is enough to move ahead and..."This information is enough to move ahead and read second part of this Java cloning tutorial, on which we will learn, how to override clone() method in Java, for classes composed with primitives, Mutable and Immutable objects in Java."<br /><br />Can't find second part!!!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-64564456778223117062014-07-08T04:13:14.918-07:002014-07-08T04:13:14.918-07:00Does Cloning makes use of reflection? If yes could...Does Cloning makes use of reflection? If yes could you please explain why and how?<br /><br />Thanks.VINODhttps://www.blogger.com/profile/17567935148510637824noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54494990196647105932014-03-20T14:27:04.839-07:002014-03-20T14:27:04.839-07:00"Anonymous said...
Return type of OVERRIDDEN ..."Anonymous said...<br />Return type of OVERRIDDEN clone method is Object in superclass, how can it be narrower i.e. Rectangle in subclass? Please correct me if I am wrong."<br />--> Java overriding rule: 'The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass'Anonymoushttps://www.blogger.com/profile/14382672089737370576noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69931678753645100552014-01-27T20:26:37.618-08:002014-01-27T20:26:37.618-08:00Return type of OVERRIDDEN clone method is Object i...Return type of OVERRIDDEN clone method is Object in superclass, how can it be narrower i.e. Rectangle in subclass? Please correct me if I am wrong. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83432531129374044142013-12-23T02:40:34.750-08:002013-12-23T02:40:34.750-08:00please provide the links for the 2nd and 3rd part ...please provide the links for the 2nd and 3rd part of the clone articleAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38285059422640401792013-11-04T23:42:08.322-08:002013-11-04T23:42:08.322-08:00HI, even hashcode is different , but equals should...HI, even hashcode is different , but equals should return true, it is not violation of equal-hashcode contract, because if two objects having same hashcode, those objects value may be same , may not be same. but when two objects having same value, both objects hashcode must be same.Sasi kumarhttps://www.blogger.com/profile/07546736101444613259noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13872117932314824882013-10-06T07:46:04.880-07:002013-10-06T07:46:04.880-07:00Why it is protected - so that you can't call i...Why it is protected - so that you can't call it on any object - the default shallow copy can be risky unless the class explicitly says its ok. Classes should override it, return the appropriate type (instead of Object) and make the visibility public. <br />http://stackoverflow.com/questions/1138769/why-is-the-clone-method-protected-in-java-lang-objectAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-19492152068394207282013-10-04T08:44:14.697-07:002013-10-04T08:44:14.697-07:00How do you relate hash code here... ?
c the hash ...How do you relate hash code here... ? <br />c the hash code calculation in above rectangle class.. its has nothing to do with the reference of the object..<br />I have a question.. Why is clone method protected by implementation ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48877907173407921032013-10-02T22:14:39.874-07:002013-10-02T22:14:39.874-07:00"a.clone() != a, which means original and clo..."a.clone() != a, which means original and clone are two separate object in Java heap, a.clone().getClass() == a.getClass() and clone.equals(a), which means clone is exact copy of original object"<br /><br />So hashcode is different but equals should return true. Isn't this a violation of the equals-hashcode contract?Anonymousnoreply@blogger.com