Open In App

Types of References in Java

Last Updated : 04 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, managing memory plays a very important role so that the program runs smoothly. One of the important parts of this is how Java handles references to objects and decides when to remove those objects that are no longer needed. Java offers several types of references, and it is important to understand that all references are not the same. In this article, we are going to discuss different types of references in Java.

Java References Types

In Java, there are four types of references differentiated by the way in which they are garbage collected.

  • Strong References
  • Weak References
  • Soft References
  • Phantom References

Prerequisite: Basic understanding of Garbage Collection

1. Strong References

This is the default type/class of Reference Object. Any object which has an active strong reference are not eligible for garbage collection. The object is garbage collected only when the variable which was strongly referenced points to null.

MyClass obj = new MyClass ();

Here 'obj' object is strong reference to newly created instance of MyClass, currently obj is active object so can't be garbage collected.

obj = null; //'obj' object is no longer referencing to the instance. So the 'MyClass type object is now available for garbage collection.

Strong References in Java

Example:

Java
// Java program to illustrate Strong reference
class Geeks
{
    //Code..
}
public class Example
{
    public static void main(String[] args)
    {
         //Strong Reference - by default
        Geeks g = new Geeks();    
        
        //Now, object to which 'g' was pointing earlier is 
        //eligible for garbage collection.
        g = null; 
    }
} 


2. Weak References

 Weak Reference Objects are not the default type/class of Reference Object and they should be explicitly specified while using them. weak-references-in-java

Example:

Java
//Java Code to illustrate Weak reference
import java.lang.ref.WeakReference;
class Gfg
{
    //code
    public void x()
    {
        System.out.println("GeeksforGeeks");
    }
}

public class Geeks
{
    public static void main(String[] args)
    {
        // Strong Reference
        Gfg g = new Gfg();     
        g.x();
        
        // Creating Weak Reference to Gfg-type object to which 'g' 
        // is also pointing.
        WeakReference<Gfg> weakref = new WeakReference<Gfg>(g);
        
        //Now, Gfg-type object to which 'g' was pointing earlier
        //is available for garbage collection.
        //But, it will be garbage collected only when JVM needs memory.
        g = null; 
        
        // You can retrieve back the object which
        // has been weakly referenced.
        // It successfully calls the method.
        g = weakref.get(); 
        
        g.x();
    }
}

Output
GeeksforGeeks
GeeksforGeeks

Note: The object may be garbage collected anytime after the strong reference is removed.

3. Soft References

In Soft reference, even if the object is free for garbage collection then also its not garbage collected, until JVM is in need of memory badly.The objects gets cleared from the memory when JVM runs out of memory.To create such references java.lang.ref.SoftReference class is used. soft-references-in-java

Example:

Java
//Code to illustrate Soft reference
import java.lang.ref.SoftReference;
class Gfg
{
    public void x()
    {
        System.out.println("GeeksforGeeks");
    }
}

public class Geeks
{
    public static void main(String[] args)
    {
        // Strong Reference
        Gfg g = new Gfg();     
        g.x();
        
        // Creating Soft Reference to Gfg-type object to which 'g' 
        // is also pointing.
        SoftReference<Gfg> softref = new SoftReference<Gfg>(g);
        
        // Now, Gfg-type object to which 'g' was pointing
        // earlier is available for garbage collection.
        g = null; 
        
        // You can retrieve back the object which
        // has been weakly referenced.
        // It successfully calls the method.
        g = softref.get(); 
        
        g.x();
    }
}

Output
GeeksforGeeks
GeeksforGeeks


4. Phantom References

The objects which are being referenced by phantom references are eligible for garbage collection. But, before removing them from the memory, JVM puts them in a queue called ‘reference queue’ . They are put in a reference queue after calling finalize() method on them.To create such references java.lang.ref.PhantomReference class is used.

Example:

Java
//Code to illustrate Phantom reference
import java.lang.ref.*;

class Gfg
{
    //code
    public void x()
    {
        System.out.println("GeeksforGeeks");
    }
}

public class Geeks
{
    public static void main(String[] args)
    {
        //Strong Reference
        Gfg g = new Gfg();     
        g.x();
        
        //Creating reference queue
        ReferenceQueue<Gfg> refQueue = new ReferenceQueue<Gfg>();

        //Creating Phantom Reference to Gfg-type object to which 'g' 
        //is also pointing.
        PhantomReference<Gfg> phantomRef = null;
        
        phantomRef = new PhantomReference<Gfg>(g,refQueue);
        
        //Now, Gfg-type object to which 'g' was pointing
        //earlier is available for garbage collection.
        //But, this object is kept in 'refQueue' before 
        //removing it from the memory.
        g = null; 
        
        //It always returns null. 
        g = phantomRef.get(); 
        
        //It shows NullPointerException.
        g.x();
    }
}

Output:

Output

Article Tags :
Practice Tags :

Similar Reads