Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Overriding clone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding clone " Watch "Overriding clone " New topic
Author

Overriding clone

Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
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 ..
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
The difference is in how the two classes hold their data. In the Stack, all the contents (the objects contained in the Stack) are referenced directly from the array named "elements". Once you clone that array, you're done. But in the HashTable, the arrays called "buckets" doesn't refer to the contents directly. Instead it holds references to another object, a HashTable.Entry. This object holds references to some of the contents, a key and a value, but it also holds a reference to another HashTable.Entry object. Which can have more contents, and more references to additional HashTable.Entry objects - they form a linked list. In order to properly clone this, you need to not just clone the buckets array, but also each HashTable.Entry object referenced along the way.
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Thanks Mike,What happens if each of the elements in elements array in Stack class refers to a linked list, assume a case where it a stack of lists , in that case we will get into the same problem right ?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
Yes. More generally, if the elements are any type of mutable object, this problem can occur.
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
yes..Thanks Mike.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overriding clone
 
Similar Threads
protected member
Iterator Pattern
immutable stack implementation
garbage collector
Implementing Clones