Reputation: 965
I was going through this tutorial and I decide to remove the static from this line of code:
private static Map map;
the code did not give any errors.. , however the weak reference did not get removed from the hashmap. Can you tell me why map must be static in order for weak reference to work ?
Upvotes: 0
Views: 121
Reputation: 7556
If you simply remove static
in
private static Map map;
your code won't compile, because non-static variable cannot be referenced from a static context
, which is the main
method in this tutorial.
Upvotes: 0
Reputation: 93599
Static means there's only one instance of that variable that every instance of that class shares. Removing the static would mean that there are more of those maps around (one per instance of the class) and that those instances wouldn't have the same data.
Weak references are totally different. They are ways to keep a reference around but still let a variable be garbage collected if nobody else needs it. The two concepts have nothing to do with each other.
Upvotes: 1