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
// Java Program to illustrate PhantomReference class
// of java.lang.ref package
// Importing PhantomReference and ReferenceQueue classes
// from java.lanf.ref package
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
// Class 1
// Helper class
class HelperClass {
// Method inside HelperClass
void Display()
{
// Print statement whenever the function is called
System.out.println("Display Function invoked ...");
}
}
// Class 2
// Main class
public class GFG {
// MyClass
// Main driver method
public static void main(String[] args)
{
// Creating a strong object of HelperClass
HelperClass obj = new HelperClass();
// Creating a reference queue of HelperClass type
ReferenceQueue<HelperClass> rq
= new ReferenceQueue<>();
// Creating a phantom reference object using rq
PhantomReference<HelperClass> pobj
= new PhantomReference<>(obj, rq);
// Display message only
System.out.println(
"-> Calling Display Function using strong object:");
// Calling the display method over the object of
// HelperClass
obj.Display();
// Display message for better readability
System.out.println("-> Object set to null");
obj = null;
// Getting elements in PhantomReference object
// using standard get() method
obj = pobj.get();
// Display status of objects after fetching
// PhantomReference class objects
System.out.println(
"-> Object status after fetching from PhantomReference now : "
+ obj);
// Display message only
System.out.println(
"-> Calling Display Function after retrieving from weak Object");
// Try block to check for exceptions
try {
obj.Display();
}
// Catch block to handle the exceptions
catch (Exception E) {
// Print message when an exception occurred
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
// Java Program to illustrate PhantomReference class
// of java.lang.ref package
// Importing Phantomreference and RefereenceQueue classes
// from java.lang.ref package
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
// Class 1
// HelperClass
class X {
// Method
// To print simply
void show()
{
// Display message whenever
// show() method is called
System.out.println("show () from X invoked..");
}
}
// Class 2
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating default object of X class
X obj = new X();
// Creating new reference queue object
ReferenceQueue<X> rq = new ReferenceQueue<X>();
// Creating an object of PhantomReference class
// of X class type with RefereneQueue object
// reference queue
PhantomReference<X> phantomobj
= new PhantomReference<X>(obj, rq);
// Display message
System.out.println(
"-> Trying to retrieve object from Phantom Reference :");
// Try block to check for exceptions
try {
// this will always throw error as it has been
// collected
// by the garbage collector
phantomobj.get().show();
}
// Catch block to handle the exceptions
catch (Exception e) {
// Print and display the exception
System.out.println(e);
}
}
}
Output-> Trying to retrieve object from Phantom Reference :
java.lang.NullPointerException
Similar Reads
java.lang.ref.Reference Class in Java java.lang.ref.Reference Class is an abstract base class for reference object. This class contains methods used to get information about the reference objects. This class is not a direct subclass because the operations on the reference objects are in close co-operation with the garbage collector. Cla
3 min read
java.lang.ref.WeakReference Class In Java When we create an object in Java, an object isn't weak by default. To create a Weak Reference Object, we must explicitly specify this to the JVM. Why Weak Reference Objects are used: Unlike C/C++, Java supports Dynamic Garbage Collection. This is performed when the JVM runs the Garbage Collector. In
3 min read
java.lang.ref.SoftReference Class in Java When we create an object in Java, an object isn't soft by default. To create a Soft Reference Object, we must explicitly specify this to the JVM. In Soft reference, even if the object is free for garbage collection then also it's not garbage collected until JVM is in need of memory badly. The object
3 min read
java.lang.ref.ReferenceQueue Class in Java A ReferenceQueue is a simple data structure onto which the garbage collector places reference objects when the reference field is cleared (set to null). You would use a reference queue to find out when an object becomes softly, weakly, or phantom reachable, so your program can take some action based
3 min read
java.lang.reflect.Proxy Class in Java A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements Seria
4 min read