java.lang.ref.Reference Class in Java
Last Updated :
20 Jul, 2022
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");
}
}
}
OutputGfg@214c265e
Object successfully enqueued
Object is enqueued
Object cleared
Object not enqueued
Similar Reads
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.reflect.Method Class in Java java.lang.reflect.Method class provides necessary details about one method on a certain category or interface and provides access for the same. The reflected method could also be a category method or an instance method (including an abstract method). This class allows broadening of conversions to oc
3 min read
java.lang.reflect.Parameter Class in Java java.lang.reflect.Parameter class provides Information about method parameters, including their name and modifiers. It also provides an alternate means of obtaining attributes for the parameter. Some useful methods of Parameter class are: public int getModifiers(): It returns the modifier flags for
4 min read
java.lang.ref.PhantomReference Class in Java 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
4 min read
java.lang.reflect.Field Class in Java The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather t
5 min read