java.lang.ref.PhantomReference Class in Java
Last Updated :
26 Oct, 2021
When we create an object in Java, an object is strong by default. To create a Phantom Reference Object, we must explicitly specify this to the JVM. Phantom Reference Objects are created as phantom reference object is eligible for garbage collection, but it is not collected instantly. Instead, it is pushed into a ReferenceQueue so that all such enqueued references can be cleared.
Constructors of this class is shown in the table below
Constructor Parameters |
Constructor Description |
PhantomReference (T,ReferenceQueue <T> q) : |
Creates a new phantom reference that refers to the given object and is registered with the given queue. It is possible to create a phantom reference with a null queue, but such a reference is completely useless: Its get method will always return null and, since it does not have a queue, it will never be enqueued. |
Methods inherited from Reference Class are as follows:
Method Name |
Method Description |
get() |
Returns this reference object’s referent. Because the referent of a phantom reference is always inaccessible, this method always returns null. |
clear() |
Clears this reference object. Invoking this method will not cause this object to be enqueued. |
enque() |
Adds this reference object to the queue with which it is registered, if any. |
isEnqueued() |
Tells whether this reference object has been enqueued, either by the program or by the garbage collector. |
Example 1:
Java
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
class HelperClass {
void Display()
{
System.out.println( "Display Function invoked ..." );
}
}
public class GFG {
public static void main(String[] args)
{
HelperClass obj = new HelperClass();
ReferenceQueue<HelperClass> rq
= new ReferenceQueue<>();
PhantomReference<HelperClass> pobj
= new PhantomReference<>(obj, rq);
System.out.println(
"-> Calling Display Function using strong object:" );
obj.Display();
System.out.println( "-> Object set to null" );
obj = null ;
obj = pobj.get();
System.out.println(
"-> Object status after fetching from PhantomReference now : "
+ obj);
System.out.println(
"-> Calling Display Function after retrieving from weak Object" );
try {
obj.Display();
}
catch (Exception E) {
System.out.println( "-> Error : " + E);
}
}
}
|
Output
-> Calling Display Function using strong object:
Display Function invoked ...
-> Object set to null
-> Object status after fetching from PhantomReference now : null
-> Calling Display Function after retrieving from weak Object
-> Error : java.lang.NullPointerException
Hence, it is seen that unlike Soft and Weak References, Phantom Reference always returns null.
Example 2:
Java
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
class X {
void show()
{
System.out.println( "show () from X invoked.." );
}
}
public class GFG {
public static void main(String[] args)
{
X obj = new X();
ReferenceQueue<X> rq = new ReferenceQueue<X>();
PhantomReference<X> phantomobj
= new PhantomReference<X>(obj, rq);
System.out.println(
"-> Trying to retrieve object from Phantom Reference :" );
try {
phantomobj.get().show();
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output
-> Trying to retrieve object from Phantom Reference :
java.lang.NullPointerException