0

I called the removeAll method to remove from my tempDictionary but it removes from d. So how can I avoid privacy leak and assign new refrence to tempDictionary.

public static void play1(Dictionary d,  int level )throws IOException
{
  Dictionary tempDictionary  = d ;
  tempDictionary.removeAll();
}
//after looking to the link doesn't compile  I tried to do exact same 

class  Dictionary {
private Dictionary dictionary; public Dictionary(Dictionary temp ) {
this.dictionary = temp.dictionary; // error 

}
} 
Anmol
  • 91
  • 1
  • 3
  • 10
  • 4
    possible duplicate of http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java – Amer Qarabsa Oct 01 '16 at 10:41
  • class Dictionary { private Dictionary dictionary; public Dictionary(Dictionary temp ) { this.dictionary = temp.dictionary; // gonna work ?? } } – Anmol Oct 01 '16 at 10:51
  • In copy constructor you should assign fields of class Dictionary, not the object itself. How do you implemented Dictionary class? – Jakub Ch. Oct 01 '16 at 11:00
  • e.g. `public Dictionary(Dictionary otherDict) { this.someField = otherDict.someField; }` – Jakub Ch. Oct 01 '16 at 11:02
  • this is my dictionary class : public class Dictionary { private ListOfNodes[] data = new ListOfNodes[26]; //Constructor public Dictionary() { inData(); AssignLetters(); } – Anmol Oct 01 '16 at 13:22

2 Answers2

0

When you do this:

   Dictionary tempDictionary  = d ;

what you actually do is that you create a second dictionary reference that points to the existing one(in your case d). So every action that you perform at tempDictionary affects d too because both point to the same object. To solve this you can copy the components of d to a new dictionary do what you want to the copy.

EDIT: I used the instance keyword in the previous answer which is wrong because a new instance is created with the new keyword.What i meant is reference.Thanks to Amer Qarabsa and Tokazio for pointing out.

theVoid
  • 743
  • 4
  • 14
  • its a variable of type Dictionary not , some may call it mistakenly reference to a Dictionary instance , but its completely wrong to say its an instance, instance = object – Amer Qarabsa Oct 01 '16 at 10:54
  • can you please tell me what u mean by copy ? – Anmol Oct 01 '16 at 10:56
  • @AmerQarabsa I don't understand what you are trying to say, please explain were i am wrong. – theVoid Oct 01 '16 at 10:57
  • @Anny I mentioned in a comment on your question a possible duplicate , you need to clone your object. – Amer Qarabsa Oct 01 '16 at 10:58
  • @Anny You can copy the components of a dictionary by value into a new dictionary so that you wont affect the original. – theVoid Oct 01 '16 at 10:59
  • @theVoid well you said "you create a second dictionary instance that points to the existing one" , this is misleading, instance = object so you are basically saying that and object of type Dictionary is pointing to another object and this is not correct. what happened here is you have a variable of type dictionary not instance – Amer Qarabsa Oct 01 '16 at 11:00
  • a second dictionary REFERENCE not instance. The 2 references are pointing to the same instance. – Tokazio Oct 01 '16 at 11:01
  • ok I got what said but problem is how can I copy each element of Dictionary class as it has a lot? should I create a class ? – Anmol Oct 01 '16 at 11:05
  • @Anny One way is to clone the object as mentioned by Amer Qarabsa. – theVoid Oct 01 '16 at 11:08
0

Use java reflection to proceed to the deep copy of the fields recursivelly. or use existing library like kryo

Tokazio
  • 516
  • 2
  • 18