I read that Java is passed by value . So lets say we have this code and say that the HashMap somehashMap
has a longer life time than foo
. So foo is not allowed to be garbage collected even though it has done its work simply because we put foo inside the Map and then forgot to remove from it . Now going by the logic of the answer in the post that I linked to we are actually passing a copy of the reference of foo
to the method put()
right ?In that case putting the foo
into the HashMap
shouldn't prevent it from getting garbage collected . Can you please help me understand what is going on here ? What am I missing exactly ?
public void someMethod(){
Foo foo = new Foo();
somehashMap.put(fooKey,foo);
}