Skip to content
Related Articles

Related Articles

Improve Article

java.lang.ref.Reference Class in Java

  • Last Updated : 17 Mar, 2021

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.

Class declaration: prevent

public abstract class Reference<T>
extends Object

Methods:

MethodDescription 
clear()This method prevents this object from being enqueued by clearing this reference object. Only java code can invoke this method. The garbage collector can directly clear references. The garbage collector doesn’t need to invoke this method for clearing references.
enqueue()This method adds this object to its registered queue.
get()This method is used to get the object to which this reference refers. It returns null if either java code or garbage collector cleared the object at this reference.
isEnqueued()This method is used to know if this reference object is registered with any queue or not.

1. public void clear(): 

This method prevents this object from being enqueued by clearing this reference object. Only java code can invoke this method. The garbage collector can directly clear references. The garbage collector doesn’t need to invoke this method for clearing references.



2. public boolean enqueue(): 

This method adds this object to its registered queue.

Returns: True if this reference object was successfully added to the registered queue, false if this reference object was not registered with any queue at the time of its creation.

3. public T get(): 

This method is used to get the object to which this reference refers. It returns null if either java code or garbage collector cleared the object at this reference.

Returns: Object to which this reference refers, null if the object was cleared.

4. public boolean isEnqueued(): 

This method is used to know if this reference object is registered with any queue or not.

Returns: True if this reference object has been enqueued, false otherwise

Java




// Java program to illustrate working of Reference Class
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
class Gfg {
}
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Strong Reference
        Gfg g = new Gfg();
  
        ReferenceQueue<Gfg> q = new ReferenceQueue<Gfg>();
  
        // Creating Soft Reference to Gfg-type object to
        // which 'g' is also pointing and registering it
        // with q
        Reference<Gfg> softref
            = new SoftReference<Gfg>(g, q);
  
        g = softref.get();
        System.out.println(g.toString());
        // enqueue softref to its rregistered queue i.e q
  
        if (softref.enqueue()) {
            System.out.println(
                "Object successfully enqueued");
        }
        else {
            System.out.println("Object not enqueued");
        }
  
        // checking if softref is enqueued or not
        if (softref.isEnqueued()) {
            System.out.println("Object is enqueued");
        }
        else {
            System.out.println("Object not enqueued");
        }
  
        // clearing this reference object
        softref.clear();
  
        System.out.println("Object cleared");
  
        // trying to enqueue after clearing
        if (softref.enqueue()) {
            System.out.println(
                "Object successfully enqueued");
        }
        else {
            System.out.println("Object not enqueued");
        }
    }
}
Output
Gfg@214c265e
Object successfully enqueued
Object is enqueued
Object cleared
Object not enqueued

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :