Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding clone

 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. More generally, if the elements are any type of mutable object, this problem can occur.
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes..Thanks Mike.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic