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 order to not waste space, the garbage collector deletes all unreachable objects. However, in order to mark an object for garbage collection, we can create a weak reference object.

Java
import java.lang.ref.WeakReference;
class GFG {
public static void main(String[] args)
{
MyClass obj = new MyClass();
WeakReference<MyClass> wobj
= new WeakReference<>(obj);
System.out.println(
"-> Calling Display Function using strong object:");
obj.Display();
System.out.println("-> Object set to null");
obj = null;
obj = wobj.get();
System.out.println(
"-> Calling Display Function after retrieving from weak Object");
obj.Display();
}
}
class MyClass {
void Display()
{
System.out.println("Display Function invoked ...");
}
}
|
Output
-> Calling Display Function using strong object:
Display Function invoked ...
-> Object set to null
-> Calling Display Function after retrieving from weak Object
Display Function invoked ...
Output
-> Calling Display Function using strong object:
Display Function invoked ...
-> Object set to null
-> Calling Display Function after retrieving from weak Object
Display Function invoked ...
Constructors in the WeakReference Class:
| Parameters |
Description |
| WeakReference ( T referent ) |
This constructor creates a new weak reference that refers to the given object. |
| WeakReference ( T referent, ReferenceQueue <T> q) |
This constructor creates a new weak reference that refers to the given object and is registered with the given queue. |
Methods inherited from Reference Class
| Method |
Description |
| clear ( ) |
This method clears this reference object. |
| enqueue ( ) |
This method adds this reference object to the queue with which it is registered if any. |
| get ( ) |
This method returns this reference object’s referent. |
| isEnqueued ( ) |
This method tells whether this reference object has been enqueued, either by the program or by the garbage collector. |
Implementation of Methods :
Java
import java.lang.ref.WeakReference;
class GFG {
public static void main(String[] args)
{
X obj = new X();
WeakReference<X> weakobj
= new WeakReference<X>(obj);
System.out.println(
"-> Retrieving object from weak reference using get ()");
weakobj.get().show();
System.out.println(
"-> Is it possible to queue object using enqueue ()");
System.out.println(weakobj.enqueue());
System.out.println(
"-> Checking if reference is queued using isEnqueued ()");
System.out.println(weakobj.isEnqueued());
}
}
class X {
void show()
{
System.out.println("show () from X invoked..");
}
}
|
Output
-> Retrieving object from weak reference using get ()
show () from X invoked..
-> Is it possible to queue object using enqueue ()
false
-> Checking if reference is queued using isEnqueued ()
false
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
10 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...