java.lang.ref.WeakReference Class In Java - GeeksforGeeks

10 Dec, 2021
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
// Java Program to show the usage of// java.lang.ref.WeakReference Classimport java.lang.ref.WeakReference;class GFG {    public static void main(String[] args)    {        // creating a strong object of MyClass        MyClass obj = new MyClass();               // creating a weak reference of type 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 ...");    }}
-> Calling Display Function using strong object:
Display Function invoked ...
-> Object set to null
-> Calling Display Function after retrieving from weak Object
Display Function invoked ...
-> 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:
Methods inherited from Reference Class
Implementation of Methods :
Java
// Java Program to show the usage of// java.lang.ref.WeakReference Classimport 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..");    }}
-> 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
 
ruhelaa48
anikakapoor
java-lang-reflect-package
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
Difference between Abstract Class and Interface in Java
How to remove an element from ArrayList in Java?