-2

I have a hashmap in which i am putting a reference to an object.

     DateTest test100 = new DateTest("ABC",100); 
     dateTestMap.put(96, test100);

I am passing this reference variable to a method where i am assigning it a different object and after returning i am pointing the existing reference to the new object as follows:

    test100 = datePassingTest(test100);

    private DateTest datePassingTest(DateTest test100)
    {
        DateTest newTest = new DateTest("XYZ", 69);
        test100 = newTest;
        return test100;
    }

Well, test100 does get modified to point to the new object but the value stored in map isn't getting updated. Is there something i am missing ?

Thanks, Adithya

Adithya
  • 2,923
  • 5
  • 33
  • 47

5 Answers5

1

That is because test100 is not holding the reference of the object which was created using this new DateTest("ABC",100);. It now points to the object created by this new DateTest("XYZ", 69);.

If you had done something like test100.setSomething(150), then the change would have been reflected in the map. In this case, it won't as the test100 now refers a completely different object.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • you mean the test100 reference and the test100 reference inside the map are 2 different copies i.e. i mean are these 2 copies which are pointing to the test100 object ? and after the method returns only the reference test100 (not the map reference) is pointing to the new object ? Does this mean that java makes a copy of the reference test100 while storing the value in the map ? – Adithya Oct 17 '13 at 07:54
  • It just stores the reference. Not a copy of the object or something. Have a look at [this answer](http://stackoverflow.com/a/12429953/2024761). It clearly explains everything. – Rahul Oct 17 '13 at 07:58
1

When you do

test100 = newTest

The only thing that is done is that test100 now references the same object as newTest. The object referenced by the original value of test100 is still present but only in the hashmap.

To do what you want, you should try

test100.setTime(newTest.getTime());
Julien
  • 2,544
  • 1
  • 20
  • 25
0
DateTest test100 = new DateTest("ABC",100); 
dateTestMap.put(96, test100);

Here a copy of reference (of test100) goes and sits into the dateTestMap which points to DateTest("ABC",100). Now a HashMap dateTestMap contains a new reference which points to the same object DateTest("ABC",100)

So it can be depicted something like this

enter image description here

Now when you do

test100 = datePassingTest(test100);

The outer test100 will point to new DateTest("XYZ", 69), but the copy reference present in dateTestMap still points to same DateTest("ABC",100) object.

enter image description here

Java is pass by value - which essentially means passing the copy of reference

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • the outer test100 and the test100 reference in the map are same, so if outer test100 is pointing to a new object the map reference should ideally be pointing to the same new bject unless map makes another copy of the reference and stores the reference ! – Adithya Oct 17 '13 at 07:50
  • when i say **reference is copied** , i am very clear that inner and outer `test100` are NOT same. – sanbhat Oct 17 '13 at 07:52
0

Thats because, you're putting the object inside the map and then modifying the object. Thus, the map would still hold the old object

 DateTest test100 = new DateTest("ABC",100); 
 dateTestMap.put(96, test100);           // entry/object added to map

 test100 = datePassingTest(test100);     // object updated

 dateTestMap.put(96, test100);           // map gets new object

// now the map contains the updated object but not the older one.
Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
0

You are not modifying the object. You are just assigning.

Clear explanation in Language Specification JLS#4.301,

If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the object, and then the altered state can be observed through the reference in the other variable.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307