java.lang.ref

Class WeakReference<T>

public class WeakReference<T> extends Reference<T>

A weak reference will be cleared, if the object is only weakly reachable. It is useful for lookup tables, where you aren't interested in an entry, if the key isn't reachable anymore. WeakHashtable is a complete implementation of such a table.
It is also useful to make objects unique: You create a set of weak references to those objects, and when you create a new object you look in this set, if the object already exists and return it. If an object is not referenced anymore, the reference will automatically cleared, and you may remove it from the set.

See Also: WeakHashMap

Constructor Summary
WeakReference(T referent)
Create a new weak reference, that is not registered to any queue.
WeakReference(T referent, ReferenceQueue<? super T> q)
Create a new weak reference.

Constructor Detail

WeakReference

public WeakReference(T referent)
Create a new weak reference, that is not registered to any queue.

Parameters: referent the object we refer to.

WeakReference

public WeakReference(T referent, ReferenceQueue<? super T> q)
Create a new weak reference.

Parameters: referent the object we refer to. q the reference queue to register on.

Throws: NullPointerException if q is null.