0

I have created a new HashMap Object say. Now I pass this object to a method where I change the reference of the variable to some other hashmap object .The reference to this new object is not reflected once the method is completed

Example :

Map<String, Boolean> map = new HashMap<String, Boolean>();
changerefofMap(HashMap<String, Boolean>() map)
{
map=methodwhichreturnSomeMap();
System.out.println(map.size());  //returns 2
}
System.out.println(map.size());  //returns 0

I don't understand why the reference is not getting changed.

2 Answers2

0

Because in java you always pass by value

sol4me
  • 15,233
  • 5
  • 34
  • 34
0

When your passing in the map object it doesn't actually use your original map. If you wanted to change the one outside of the method you should just reference it directly.

Le Ish Man
  • 451
  • 2
  • 4
  • 20