Question on clone :
Hi,
I am reading JOsh Bloch's effective java (2nd edition) , I have a question in
the topic 'Override clone judiciously' (Item 11) page no: 57
It provides an example of a java class providing a stack functionality. The
author cautions that merely calling super.clone() will result in the cloned
Stack's array holding the elements referring to the same array as the Original
Stack instance. Modifying the original will destroy the invariants in the clone
and vice versa.
He suggests that to avoid this situation we have to call clone on the array
recursively.
I am able to understand till this point.
Now he cites another example
public class HashTable implements Cloneable {
private Entry[] buckets = ...;
private static class Entry {
final Object key;
Object value;
Entry next;
Entry(Object key, Object value, Entry next) {
this.key = key;
this.value = value;
this.next = next;
}
}
He cautions that the strategy that is used in cloning the Stack class does not work here..
/ Broken - results in shared internal state!
"Though the clone has its own bucket array, this array references the same linked lists as the original"
I am not able to understand why the same problem is not applicable for the Stack class .
Please help me understand this concept ..