Open In App

java.lang.ref.Reference Class in Java

Last Updated : 20 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Method Description 
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 registered 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads