The short version of the question:
I do: x = y
. Then I change x
, and y
is unchanged. What I want is to "bind" x
and y
in such a way that I change y
whenever I change x
.
The extended version (with some details):
I wrote a class ("first" class) which generates objects of another class ("second" class). In more details, every object of the second class has a name as a unique identifier. I call a static method of the first class with a name of the object from the second class. The first class checks if such an object was already generated (if it is present in the static HashMap of the first class). If it is already there, it is returned. If it is not yet there, it is created, added to the HashMap and returned.
And then I have the following problem. At some stage of my program, I take an object with a specific name from the HashMap of the first class. I do something with this object (for example change values of some fields). But the object in the HashMap does not see these changes! So, in fact, I do not "take" an object from the HashMap, I "create a copy" of this object and this is what I would like to avoid.
ADDED:
As it was mentioned in the answers, I should not have the described behavior. And I actually do not have it (I misinterpreted the behavior of my program). I made an mistake with name of objects. I wanted to refer to an existing object by its name and I used a wrong name, so I actually created a new object and it's why I did not see any changes that I made to the old object.
But anyway, I learned that if I take an object from a HashMap and do some changes to this object, they will be also applied to the object "sitting" in the HashMap. So, I can gave different references to the same object and I can use any of these references to change the state of the object.
Thank you for your answers.