Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/java/lang/ref/Reference#pubmethods) \| [Protected Methods](https://developer.android.com/reference/java/lang/ref/Reference#promethods) \| [Inherited Methods](https://developer.android.com/reference/java/lang/ref/Reference#inhmethods)

# Reference

` public abstract class Reference `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.lang.ref.Reference\<T\> |

Known direct subclasses [PhantomReference](https://developer.android.com/reference/java/lang/ref/PhantomReference)\<T\>, [SoftReference](https://developer.android.com/reference/java/lang/ref/SoftReference)\<T\>, [WeakReference](https://developer.android.com/reference/java/lang/ref/WeakReference)\<T\> [PhantomReference](https://developer.android.com/reference/java/lang/ref/PhantomReference)\<T\> Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. [SoftReference](https://developer.android.com/reference/java/lang/ref/SoftReference)\<T\> Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand. [WeakReference](https://developer.android.com/reference/java/lang/ref/WeakReference)\<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.

## Summary

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/java/lang/ref/Reference#clear()() ` Clears this reference object. |
| ` boolean` | ` https://developer.android.com/reference/java/lang/ref/Reference#enqueue()() ` Adds this reference object to the queue with which it is registered, if any. |
| ` T` | ` https://developer.android.com/reference/java/lang/ref/Reference#get()() ` Returns this reference object's referent. |
| ` boolean` | ` https://developer.android.com/reference/java/lang/ref/Reference#isEnqueued()() ` *This method was deprecated in API level 31. This method was never implemented to test if a reference object has been cleared and enqueued as it was previously specified since 1.2. This method could be misused due to the inherent race condition or without an associated `ReferenceQueue`. An application relying on this method to release critical resources could cause serious performance issue. An application should use `https://developer.android.com/reference/java/lang/ref/ReferenceQueue` to reliably determine what reference objects that have been enqueued or `refersTo(null)` to determine if this reference object has been cleared.* |
| ` static void` | ` https://developer.android.com/reference/java/lang/ref/Reference#reachabilityFence(java.lang.Object)(https://developer.android.com/reference/java/lang/Object ref) ` Ensures that the object referenced by the given reference remains [*strongly reachable*](https://developer.android.com/reference/java/lang/ref/package-summary#reachability), regardless of any prior actions of the program that might otherwise cause the object to become unreachable; thus, the referenced object is not reclaimable by garbage collection at least until after the invocation of this method. |
| ` final boolean` | ` https://developer.android.com/reference/java/lang/ref/Reference#refersTo(T)(T obj) ` Tests if the referent of this reference object is `obj`. |

| ### Protected methods ||
|---|---|
| ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/ref/Reference#clone()() ` Throws `https://developer.android.com/reference/java/lang/CloneNotSupportedException`. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public methods

### clear

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

### enqueue

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Returns ||
|---|---|
| `boolean` | `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 <br /> |

### get

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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`.

<br />

| Returns ||
|---|---|
| `T` | The object to which this reference refers, or `null` if this reference object has been cleared <br /> |

### isEnqueued

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isEnqueued ()
```

<br />

**This method was deprecated in API level 31.**   

This method was never implemented to test if a reference object has been cleared and enqueued as it was previously specified since 1.2. This method could be misused due to the inherent race condition or without an associated `ReferenceQueue`. An application relying on this method to release critical resources could cause serious performance issue. An application should use `https://developer.android.com/reference/java/lang/ref/ReferenceQueue` to reliably determine what reference objects that have been enqueued or `refersTo(null)` to determine if this reference object has been cleared.

Tests if this reference object is in its associated queue, if any. This method returns `true` only if all of the following conditions are met:

- this reference object was registered with a queue when it was created; and
- the garbage collector has added this reference object to the queue or `https://developer.android.com/reference/java/lang/ref/Reference#enqueue()` is called; and
- this reference object is not yet removed from the queue.

Otherwise, this method returns `false`. This method may return `false` if this reference object has been cleared but not enqueued due to the race condition.

<br />

<br />

| Returns ||
|---|---|
| `boolean` | `true` if and only if this reference object is in its associated queue (if any). <br /> |

### reachabilityFence

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static void reachabilityFence (Object ref)
```

Ensures that the object referenced by the given reference remains [*strongly reachable*](https://developer.android.com/reference/java/lang/ref/package-summary#reachability), regardless of any prior actions of the program that might otherwise cause the object to become unreachable; thus, the referenced object is not reclaimable by garbage collection at least until after the invocation of this method. Invocation of this method does not itself initiate garbage collection or finalization.

This method establishes an ordering for [*strong reachability*](https://developer.android.com/reference/java/lang/ref/package-summary#reachability) with respect to garbage collection. It controls relations that are otherwise only implicit in a program -- the reachability conditions triggering garbage collection. This method is designed for use in uncommon situations of premature finalization where using `synchronized` blocks or methods, or using other synchronization facilities are not possible or do not provide the desired control. This method is applicable only when reclamation may have visible effects, which is possible for objects with finalizers (See [Section 12.6 17 of The Java™ Language Specification](https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.6)) that are implemented in ways that rely on ordering control for correctness.

<br />

##### API Note:

- Finalization may occur whenever the virtual machine detects that no reference to an object will ever be stored in the heap: The garbage collector may reclaim an object even if the fields of that object are still in use, so long as the object has otherwise become unreachable. This may have surprising and undesirable effects in cases such as the following example in which the bookkeeping associated with a class is managed through array indices. Here, method `action` uses a `reachabilityFence` to ensure that the `Resource` object is not reclaimed before bookkeeping on an associated `ExternalResource` has been performed; in particular here, to ensure that the array slot holding the `ExternalResource` is not nulled out in method `https://developer.android.com/reference/java/lang/Object#finalize()`, which may otherwise run concurrently.

      class Resource {
        private static ExternalResource[] externalResourceArray = ...

        int myIndex;
        Resource(...) {
          myIndex = ...
          externalResourceArray[myIndex] = ...;
          ...
        }
        protected void finalize() {
          externalResourceArray[myIndex] = null;
          ...
        }
        public void action() {
          try {
            // ...
            int i = myIndex;
            Resource.update(externalResourceArray[i]);
          } finally {
            Reference.reachabilityFence(this);
          }
        }
        private static void update(ExternalResource ext) {
          ext.status = ...;
        }
      }

  Here, the invocation of `reachabilityFence` is nonintuitively placed *after* the call to `update`, to ensure that the array slot is not nulled out by `https://developer.android.com/reference/java/lang/Object#finalize()` before the update, even if the call to `action` was the last use of this object. This might be the case if, for example a usage in a user program had the form `new Resource().action();` which retains no other reference to this `Resource`. While probably overkill here, `reachabilityFence` is placed in a `finally` block to ensure that it is invoked across all paths in the method. In a method with more complex control paths, you might need further precautions to ensure that `reachabilityFence` is encountered along all of them.

  It is sometimes possible to better encapsulate use of `reachabilityFence`. Continuing the above example, if it were acceptable for the call to method `update` to proceed even if the finalizer had already executed (nulling out slot), then you could localize use of `reachabilityFence`:

      public void action2() {
        // ...
        Resource.update(getExternalResource());
      }
      private ExternalResource getExternalResource() {
        ExternalResource ext = externalResourceArray[myIndex];
        Reference.reachabilityFence(this);
        return ext;
      }

  Method `reachabilityFence` is not required in constructions that themselves ensure reachability. For example, because objects that are locked cannot, in general, be reclaimed, it would suffice if all accesses of the object, in all methods of class `Resource` (including `finalize`) were enclosed in `synchronized (this)` blocks. (Further, such blocks must not include infinite loops, or themselves be unreachable, which fall into the corner case exceptions to the "in general" disclaimer.) However, method `reachabilityFence` remains a better option in cases where this approach is not as efficient, desirable, or possible; for example because it would encounter deadlock.

| Parameters ||
|---|---|
| `ref` | `Object`: the reference. If `null`, this method has no effect. <br /> |

### refersTo

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public final boolean refersTo (T obj)
```

Tests if the referent of this reference object is `obj`. Using a `null` `obj` returns `true` if the reference object has been cleared. Prefer this to a comparison with the result of `get`.

<br />

| Parameters ||
|---|---|
| `obj` | `T`: the object to compare with this reference object's referent <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if `obj` is the referent of this reference object <br /> |

## Protected methods

### clone

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected Object clone ()
```

Throws `https://developer.android.com/reference/java/lang/CloneNotSupportedException`. A `Reference` cannot be meaningfully cloned. Construct a new `Reference` instead.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/Object` | never returns normally <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/CloneNotSupportedException` | always |