Can I have two names for the same variable?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-19T09:27:22Z Indexed on 2010/03/19 9:31 UTC
Read the original article Hit count: 223

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about oop