Reputation: 71188
if I have something like this
Map<String, Foo> foos;
...
Foo f = foos.get("key1");
foos.removeAll();
Am I still able to do String s = f.getSomeProperty();
Upvotes: 1
Views: 197
Reputation: 17459
Yes f is Foo object and if Foo have some property that is string use it
Code:
Map<String, Integer> mp = new HashMap<String, Integer>();
mp.put("key1", 48);
mp.put("key2", 50);
mp.put("key3", 52);
mp.put("key4", 54);
mp.put("key5", 56);
mp.put("key6", 58);
Integer mytemp = mp.get("key1");
mp.clear();
System.out.println(mytemp);
Out:
48
that see mytemp is your f that is full object not only pointer
Upvotes: 1
Reputation: 3419
The simple answer is yes. f
contains a reference to the object that foos.Get("key1")
returned (assuming it is non-null). When you remove all from foos
, you're simply removing the references from the foos
object - you are not actually destroying the data that foos
used to contain.
Upvotes: 1
Reputation: 1207
Note that String (note the capital S) is the name of the Java class which represents character strings. But yes, yes you are.
Upvotes: 1
Reputation: 30985
foos
holds only references to objects, not full objects. If you get something from foos
, you get reference to that object and you can modify it. If you remove all elements from foos
, then it no longer holds any reference, and those objects can be garbage collected only if there is no other references to that objects.
Upvotes: 11
Reputation: 114757
Yes, you are. You just cleared the map, but the local variable f
still holds a valid reference to the Foo
and you still can use it.
Upvotes: 6