Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 691075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:30:07+00:00 2026-05-14T02:30:07+00:00

The short version of the question: I do: x = y . Then I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:30:08+00:00Added an answer on May 14, 2026 at 2:30 am

    Well, you can’t alias variables. However, you should be aware that if you make changes to an object which is referred to by both x and y, then those changes will be visible via either variable. The value of a reference type variable is a reference, not the object itself.

    It sounds like this should be the case in your situation – unless you’ve explicitly put some cloning in place, there should only actually be one object per name, and any changes you make to that object via one variable should be visible via others or via the map. Object cloning doesn’t just happen automatically.

    I suggest you try to create a short but complete example program which demonstrates the problem – I suspect that in the process of doing so, you’ll find a bug in your code which explains the behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version question : Is there navigator.mozIsLocallyAvailable equivalent function that works on all browsers,
I have a short version of the question: I start a thread like that:
The short version of my question is how do I change the build order
The short version of this question is: How can I take data that only
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
The short version of the question - why can't I do this? I'm restricted
This is basically a rails 3 version of this question . Short of parsing
Short version: I'm trying to determine the best way to track what the user
Short version of question: see title Long version of question: I've used jquery's show()
Short version of the question: Since I already have TortoiseHg, I right clicked on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.