# Reference

public abstract class **Reference** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  

|---|---|---|
| Known Direct Subclasses [PhantomReference](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/PhantomReference.html)\<T\>, [SoftReference](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/SoftReference.html)\<T\>, [WeakReference](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/WeakReference.html)\<T\> |---|---| | [PhantomReference](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/PhantomReference.html)\<T\> | Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. | | [SoftReference](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/SoftReference.html)\<T\> | Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand. | | [WeakReference](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/WeakReference.html)\<T\> | Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. | |||

Abstract base class for reference objects. This class defines the
operations common to all reference objects. Because reference objects are
implemented in close cooperation with the garbage collector, this class may
not be subclassed directly.

### Public Method Summary

|---|---|
| void | [clear](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/Reference.html#clear())() Clears this reference object. |
| boolean | [enqueue](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/Reference.html#enqueue())() Adds this reference object to the queue with which it is registered, if any. |
| T | [get](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/Reference.html#get())() Returns this reference object's referent. |
| boolean | [isEnqueued](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/Reference.html#isEnqueued())() Tells whether or not this reference object has been enqueued, either by the program or by the garbage collector. |

### Protected Method Summary

|---|---|
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/Reference.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Public Methods

#### public void
**clear**
()

Clears this reference object. Invoking this method will not cause this
object to be enqueued.

This method is invoked only by Java code; when the garbage collector
clears references it does so directly, without invoking this method.

#### public boolean
**enqueue**
()

Adds this reference object to the queue with which it is registered,
if any.

This method is invoked only by Java code; when the garbage collector
enqueues references it does so directly, without invoking this method.

##### Returns

- `true` if this reference object was successfully enqueued; `false` if it was already enqueued or if it was not registered with a queue when it was created

#### public T
**get**
()

Returns this reference object's referent. If this reference object has
been cleared, either by the program or by the garbage collector, then
this method returns `null`.

##### Returns

- The object to which this reference refers, or `null` if this reference object has been cleared

#### public boolean
**isEnqueued**
()

Tells whether or not this reference object has been enqueued, either by
the program or by the garbage collector. If this reference object was
not registered with a queue when it was created, then this method will
always return `false`.

##### Returns

- `true` if and only if this reference object has been enqueued

## Protected Methods

#### protected void
**finalize**
()

Invoked when the garbage collector has detected that this instance is no longer reachable.
The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override `finalize` are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit `close` method (and implement
`https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html`), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a `BigInteger`
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.

If you *must* use finalizers, consider at least providing your own
`https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/ref/ReferenceQueue.html` and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for
calling `super.finalize()` yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer
thread.

See *Effective Java* Item 7, "Avoid finalizers" for more.