Java SE API

PhantomReference

Supports post-reachability notification; get always returns null.

Formal Syntax & Structure

java syntaxjava
new PhantomReference<>(value, queue)

Working Example

Example Snippetjava
public class Main {
    public static void main(String[] args) throws Exception {        Object value = new Object();        var soft = new java.lang.ref.SoftReference<>(value);        var phantom = new java.lang.ref.PhantomReference<>(value, new java.lang.ref.ReferenceQueue<>());        System.out.println(soft.get() == value);        System.out.println(phantom.get() == null);        java.lang.ref.Reference.reachabilityFence(value);    }}
Java Reference