T - the type of the referentPhantomReference, SoftReference, WeakReferencepublic abstract sealed class Reference<T> extends Object permits PhantomReference<T>, SoftReference<T>, WeakReference<T> (not exhaustive)
| Modifier and Type | Method | Description |
|---|---|---|
void |
clear() |
Clears this reference object. |
protected Object |
clone() |
Throws CloneNotSupportedException. |
boolean |
enqueue() |
Clears this reference object, then attempts to add it to the queue with which it is registered, if any. |
T |
get() |
Returns this reference object's referent. |
boolean |
isEnqueued() |
Deprecated. This method was originally specified to test if a reference object has been cleared and enqueued but was never implemented to do this test. |
static void |
reachabilityFence |
Ensures that the given object remains strongly reachable. |
final boolean |
refersTo |
Tests if the referent of this reference object is obj. |
public T get()
null.refersTo method can be used to avoid such strengthening when testing whether some object is the referent of a reference object; that is, use ref.refersTo(obj) rather than ref.get() == obj.null if this reference object has been clearedpublic final boolean refersTo(T obj)
obj. Using a null obj returns true if the reference object has been cleared.obj - the object to compare with this reference object's referenttrue if obj is the referent of this reference objectpublic void clear()
When the garbage collector or the enqueue() method clear references they do so directly, without invoking this method.
reachabilityFence(Object) if necessary.@Deprecated(since="16") public boolean isEnqueued()
ReferenceQueue. An application relying on this method to release critical resources could cause serious performance issue. An application should use ReferenceQueue to reliably determine what reference objects that have been enqueued or refersTo(null) to determine if this reference object has been cleared.true only if all of the following conditions are met: enqueue() is called; and false. This method may return false if this reference object has been cleared but not enqueued due to the race condition.true if and only if this reference object is in its associated queue (if any).public boolean enqueue()
If this reference is registered with a queue but not yet enqueued, the reference is added to the queue; this method is successful and returns true. If this reference is not registered with a queue, or was already enqueued (by the garbage collector, or a previous call to enqueue), this method is unsuccessful and returns false.
Memory consistency effects: Actions in a thread prior to a successful call to enqueue happen-before the reference is removed from the queue by ReferenceQueue.poll() or ReferenceQueue.remove(long). Unsuccessful calls to enqueue have no specified memory consistency effects.
When this method clears references it does so directly, without invoking the clear() method. When the garbage collector clears and enqueues references it does so directly, without invoking the clear() method or this method.
ReferenceQueue.poll() and ReferenceQueue.remove(long) methods to return this reference even though the referent may still be strongly reachable.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 createdprotected Object clone() throws CloneNotSupportedException
CloneNotSupportedException. A Reference cannot be meaningfully cloned. Construct a new Reference instead.clone in class Object
CloneNotSupportedException - alwayspublic static void reachabilityFence(Object ref)
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 applicable only when reclamation may have visible effects, such as for objects that use finalizers or Cleaner, or code that performs reference processing.
Memory consistency effects: Actions in a thread prior to calling reachabilityFence(x) happen-before the garbage collector clears any reference to x.
reachabilityFence can prevent this race by ensuring that the object remains strongly reachable. The following is an 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; specifically, to ensure that the array slot holding the ExternalResource is not nulled out in method Object.finalize(), which may otherwise run concurrently.
class Resource {
private static ExternalResource[] externalResourceArray = ...
int myIndex;
Resource(...) {
this.myIndex = ...
externalResourceArray[myIndex] = ...;
...
}
protected void finalize() {
externalResourceArray[this.myIndex] = null;
...
}
public void action() {
try {
// ...
int i = this.myIndex; // last use of 'this' Resource in action()
Resource.update(externalResourceArray[i]);
} finally {
Reference.reachabilityFence(this);
}
}
private static void update(ExternalResource ext) {
ext.status = ...;
}
}
reachabilityFence is placed after the call to update, to ensure that the array slot is not nulled out by 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. The reachabilityFence call is placed in a finally block to ensure that it is invoked across all paths in the method. A more complex method might need further precautions to ensure that reachabilityFence is encountered along all code paths. 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 synchronization is not as efficient, desirable, or possible; for example because it would encounter deadlock.
ref - the reference to the object to keep strongly reachable. If null, this method has no effect.
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/ref/Reference.html