Object Hash Code IDs and References
Hi all,
Here is my problem. I have an object, "MasterObj" for discussion sake, that I instantiate once at start-up, from a file contains a list of properties. The properties are used to set various fields in the MasterObj. During run time, other objects, "Callers", are going to have sessions with MasterObj which are described as thus. An object will take a copy of MasterObj and run a loop that will maniulate MO's fields. After the loop is over, the Caller object returns what the state of the MO's fields are. After the session is over, MO needs to return to its original state so that the other Caller objects can have sessions with it. Keep this is mind:
- When I say MO needs to return to its "original state", I don't mean its compiled value (static fields, etc.). What I mean is I set MO up at the begining of RunTime, and I do this by reading in properties from a file. This is the state MO must return to after each Caller has its session.
- This might seem to have Object.clone() written all over it, but that want do any good here. MasterObj and the fields in MasterObj are HashMaps. From what I understand, a HashMap will map an identifer with one instance of an object. So if I do:
String x = "Hello.";
hashmap.put("greeting", x);
String y = (String) hashmap.get("greeting");
y += " What is your name";
System.out.print(x);
_
Here, the console will print "Hello. What is your name". Try it. The reason is because the VM maped 'y' to the same Hash code ID of 'x', so 'y' is just another way to reference the same instance of that object. What I want to do is something like:
String y = (String) hashmap.get("greeting").clone();
_
But the clone() on a HashMap only returns a "shallow" copy, so it doesn't copy the keys and values. What I need is a way to make a *real* copy of whatever is returned by 'hashmap.get()', so that I can manipulate it without affecting what is stored in 'hashmap'.
I was hoping that someone else has already encountered this problem and could provide me with a way to get a solution. Thanks
Sherman

