• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overriding clone

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 5161
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes..Thanks Mike.
reply
    Bookmark Topic Watch Topic
  • New Topic