Google is committed to advancing racial equity for Black communities. See how.
Added in API level 1

Reference

abstract class Reference<T : Any!>
kotlin.Any
   ↳ java.lang.ref.Reference

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

open Unit

Clears this reference object.

open Boolean

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

open T?
get()

Returns this reference object's referent.

open Boolean

Tells whether or not this reference object has been enqueued, either by the program or by the garbage collector.

open static Unit

Ensures that the object referenced by the given reference remains strongly reachable, 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.

Public methods

clear

Added in API level 1
open fun clear(): Unit

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.

enqueue

Added in API level 1
open fun enqueue(): Boolean

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.

Return
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

get

Added in API level 1
open fun get(): T?

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.

Return
T? The object to which this reference refers, or null if this reference object has been cleared

isEnqueued

Added in API level 1
open fun isEnqueued(): Boolean

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.

Return
Boolean true if and only if this reference object has been enqueued

reachabilityFence

Added in API level 28
open static fun reachabilityFence(ref: Any!): Unit

Ensures that the object referenced by the given reference remains strongly reachable, 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 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) that are implemented in ways that rely on ordering control for correctness.

Parameters
ref Any!: the reference. If null, this method has no effect.