public class WeakRef extends Object
You specify whether it should be a weak or soft reference in the constructor. The difference between the two is that if an Object is only reachable by Weak references then the Object will be garbage collected and the weak references cleared as soon as possible. An object that is soft referenced will not be immediately garbage collected but may be kept until a later time. The only guarantee is that should the VM run out of memory, all soft references will be cleared and their objects garbage collected before the VM throws an out of memory error.
Constructor and Description |
---|
WeakRef()
Create a WeakRef initially set to be referencing nothing.
|
WeakRef(boolean isSoft)
Create a WeakRef initially set to be referencing nothing which may
be soft or weak.
|
WeakRef(Object forWho)
Create a WeakRef initially set to be referencing an Object.
|
WeakRef(Object forWho,
boolean isSoft)
Create a WeakRef initially set to be referencing an Object.
|
Modifier and Type | Method and Description |
---|---|
WeakRef |
clear()
Set the WeakRef to reference nothing.
|
static WeakRef |
clear(WeakRef wr) |
Object |
get()
Get the object being referred to as long as it has not been cleared.
|
static Object |
get(WeakRef from) |
boolean |
isSoftRef()
Returns if this Ref is a soft reference.
|
WeakRef |
set(Object who)
Set the WeakRef to reference a different object (which may be null).
|
static WeakRef |
set(WeakRef to,
Object newObject) |
public WeakRef(Object forWho, boolean isSoft)
forWho
- the object to reference (which may be null).isSoft
- set true for this to act as a soft reference.public WeakRef(Object forWho)
forWho
- the object to reference (which may be null).public WeakRef(boolean isSoft)
isSoft
- set true for this to act as a soft reference.public WeakRef()
public boolean isSoftRef()
public WeakRef set(Object who)
who
- the new Object to reference.public WeakRef clear()
public Object get()