Java : Reference with Examples

Reference (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Reference methods.


Summary

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.

Class diagram

// An example with WeakReference.

var sb = new StringBuilder("abcd");
final Reference<StringBuilder> ref = new WeakReference<>(sb);

System.out.println(ref.get()); // abcd

System.gc();
System.out.println(ref.get()); // abcd

sb = null;
System.out.println(ref.get()); // abcd

System.gc();
System.out.println(ref.get()); // null

Methods

void clear ()

Clears this reference object.

final var sb = new StringBuilder("abcd");
final Reference<StringBuilder> ref = new WeakReference<>(sb);

System.out.println(ref.get()); // abcd

ref.clear();
System.out.println(ref.get()); // null
final var sb = new StringBuilder("abcd");

final var queue = new ReferenceQueue<StringBuilder>();
final Reference<StringBuilder> ref = new WeakReference<>(sb, queue);

System.out.println(ref.get()); // abcd

ref.clear();
System.out.println(ref.get()); // null

// No queue.
System.out.println(queue.poll()); // null

protected Object clone ()

Throws CloneNotSupportedException.

final var sb = new StringBuilder("abcd");
final var ref = new WeakReference<StringBuilder>(sb) {
    public void test() {
        try {
            clone();
        } catch (CloneNotSupportedException e) {
            System.out.println("CloneNotSupportedException!");
        }
    }
};

ref.test();

// Result
// ↓
//CloneNotSupportedException!

boolean enqueue ()

Clears this reference object and adds it to the queue with which it is registered, if any.

final var sb = new StringBuilder("abcd");

final var queue = new ReferenceQueue<StringBuilder>();
final Reference<StringBuilder> ref = new WeakReference<>(sb, queue);

System.out.println(ref.get()); // abcd

final var ret = ref.enqueue();
System.out.println(ret); // true
System.out.println(ref.get()); // null
System.out.println(queue.poll()); // java.lang.ref.WeakReference@7625b376
final var sb = new StringBuilder("abcd");
final Reference<StringBuilder> ref = new WeakReference<>(sb);

System.out.println(ref.get()); // abcd

final var ret = ref.enqueue();
System.out.println(ret); // false
System.out.println(ref.get()); // null

T get ()

Returns this reference object's referent.

var sb = new StringBuilder("abcd");
final Reference<StringBuilder> ref = new WeakReference<>(sb);

System.out.println(ref.get()); // abcd

sb = null;
System.out.println(ref.get()); // abcd

System.gc();
System.out.println(ref.get()); // null

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.

Deprecated.

static void reachabilityFence (Object ref)

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.

I haven't fully confirmed the behavior yet, so I don't have a code example ready. (I may update it later...)

final boolean refersTo (T obj)

Tests if the referent of this reference object is obj.

var sb1 = new StringBuilder("abcd");
final Reference<StringBuilder> ref = new WeakReference<>(sb1);

System.out.println(ref.refersTo(sb1)); // true

var sb2 = new StringBuilder("XYZ");
System.out.println(ref.refersTo(sb2)); // false

Related posts

To top of page