Hi all,
For a class with an array field Josh says if the clone method merely returns super.clone(), the resulting class instance will have the correct values in primitive fields, but its array field will refer to the same array as the original class instance. Modifying the original will destroy the invariants and vice-versa.
He used the example of custom Stack implementation, I am using a simple Student class
Please note: I didn't invoke clone() on my array field in my clone method's override.
Then I did:
Output:
I was wondering that changing array in original instance would change the array in my clone too, because I mentioned above "array field will refer to the same array as the original instance"
With my implementation of clone I should have seeing changes in the clone s2 too. The proper implementation would've been:
Have I misunderstood this? Can someone please explain what is going on?
Thanks
~Mohit