Can I have two names for the same variable?

Short version of the question:

I'm doing: x = y. Then I change x, ynot change. I want to "tie" xand yin a way that I changed yevery time I change x.

Extended version (with some details):

I wrote a class (the "first" class) that generates objects of another class (the "second" class). In more detail, each object of the second class has a name as a unique identifier. I call the static method of the first class with the name of the object from the second class. The first class checks if such an object has already been created (if it is present in the first-class static HashMap). If he already exists, he returns. If it does not already exist, it is created, added to the HashMap, and returned.

And then I have the following problem. At some point in my program, I take an object with a specific name from the first-class HashMap. I am doing something with this object (for example, changing the 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 I would like to avoid this.

ADDED:

As mentioned in the answers, I should not describe the behavior. And I don’t have it at all (I misinterpreted the behavior of my program). I made a mistake with the name of the objects. I wanted to reference an existing object by its name, and I used the wrong name, so I actually created a new object, and therefore I did not see any changes that I made to the old object.

, , HashMap , , "" HashMap. , , .

.

+3
5

, . , , x, y, . , .

, - , , , , . .

, , - , , .

+7

, . , , , , - .

+1

, =.

, x y. , y, obejct .

, addd. x y , add .

, , , , String. String, , +, Java . , .

+1

- HashMap . - (, ). HashMap ! , , "" HashMap, " " , .

HashMap get() . , , ; , X Y, , . ; Java Concurrency In Practice.

+1
source

Use JavaFX:

var y = 10;
def x = bind y;

println(x); // prints 10

y = 12;

println(x); // prints 12

Ok, not the answer to your problem, but I like the ^^ binding mechanism.

0
source

Source: https://habr.com/ru/post/1737658/


All Articles