Table of contents
1.
Introduction
2.
What is the Destructor in Java?
3.
Do Destructors Exist in Java?
3.1.
Why Java Doesn’t Need Destructors
3.2.
The finalize() Method (Deprecated)
3.2.1.
Example (Deprecated):
3.3.
Alternatives to Destructors in Java
3.4.
Resource Cleanup Mechanism 1: try-with-resources
3.4.1.
Example:
3.5.
Resource Cleanup Mechanism 2: finally Block
3.5.1.
Example:
4.
How does Java Destructor work?
5.
Advantages of Destructor
6.
Disadvantages of Destructor
7.
Java Finalize() Method
7.1.
Syntax
7.2.
Implementation
7.3.
Code
7.4.
Java
7.5.
Output
8.
Example of Destructor
8.1.
Implementation
8.2.
Java
8.3.
Output
9.
Java Destructor vs Constructor: Key Differences
9.1.
What is a Constructor in Java?
9.1.1.
Example – Constructor Usage:
9.2.
Does Java Have Destructors?
9.2.1.
Example – Simulating Destructor Behavior:
9.3.
Comparison Table: Java Constructor vs (Simulated) Destructor
10.
Frequently Asked Questions
10.1.
What is deconstructor in Java?
10.2.
Does Java have a deconstructor?
10.3.
What is an example of a destructor?
10.4.
What is the difference between constructor and destructor?
10.5.
What is a destructor method?
11.
Conclusion
Last Updated: Jun 26, 2025
Easy

Java Destructor

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A destructor in java is a method invoked during an object's destruction, primarily to release allocated memory and manage resources such as closing files, database connections, and network resources. Its main purpose is to ensure proper cleanup when an object goes out of scope.

java destructor

In this article, we will understand the destructor concept of Java (Java Destructor) and how we can utilize or implement a Java Destructor.

What is the Destructor in Java?

As we are already familiar with the concept of Destructor. When an object's life cycle is complete, a destructor, a specific procedure, is automatically invoked. To release memory that has been allocated, a destructor is called. We will waste no time in knowing what Java Destructor is and what its use is in memory management. Let us know if there is any need for a destructor in java. To free up memory, Java's garbage collector automatically deletes useless objects. There is no need for developers to designate objects for deletion, as doing so is error-prone and susceptible to memory leaks. Java doesn't have any destructors, which makes sense. So, there is no concept of destructor in java. Finalizers, also referred to as destructors in Java, are non-deterministic. In Java, the garbage collector inherently handles memory allocation and release.

Unlike C# finalizers, which are invoked at the .NET run-time, Java finalizers must be implicitly invoked because their invocation is not guaranteed. Java provides a garbage collector that works similarly to a destructor. A software known as a "garbage collector" runs on the Java virtual machine to free up memory by getting rid of things that are no longer in use or have reached the end of their useful lives. An item is only considered eligible for rubbish collection if it cannot be reached.

Do Destructors Exist in Java?

No, Java does not have destructors like C++. In C++, destructors are used to explicitly free memory or resources when an object goes out of scope. However, Java handles memory management differently through automatic garbage collection. The Java Virtual Machine (JVM) automatically identifies and deletes objects that are no longer referenced, making explicit destructors unnecessary.

Why Java Doesn’t Need Destructors

Java uses a garbage collector to manage memory. This means you don’t need to manually destroy objects or free memory. The garbage collector automatically reclaims memory used by objects that are no longer reachable in the program. This reduces the chances of memory leaks and simplifies development.

The finalize() Method (Deprecated)

Java once offered the finalize() method as a way to perform cleanup before an object is garbage collected. However, finalize() is now deprecated as of Java 9 and removed in newer versions due to performance issues and unpredictability.

Example (Deprecated):

@Override
protected void finalize() throws Throwable {
    System.out.println("Cleanup before garbage collection");
}
You can also try this code with Online Java Compiler
Run Code

Avoid using finalize() in new code. It is unreliable and has been deprecated.

Alternatives to Destructors in Java

Java provides modern and reliable mechanisms for resource management:

  • try-with-resources
  • finally blocks
  • Implementing AutoCloseable

These methods ensure proper cleanup of resources like files, streams, or database connections without depending on garbage collection timing.

Resource Cleanup Mechanism 1: try-with-resources

The try-with-resources statement is used to automatically close resources that implement the AutoCloseable interface. When the try block completes, the resource is closed automatically, even if an exception occurs.

Example:

try (BufferedReader reader = new BufferedReader(new FileReader("data.txt"))) {
    System.out.println(reader.readLine());
} catch (IOException e) {
    e.printStackTrace();
}
You can also try this code with Online Java Compiler
Run Code

This approach is clean, concise, and preferred for resource management.

Resource Cleanup Mechanism 2: finally Block

The finally block is used when you need to guarantee that specific code runs after a try or catch block, regardless of whether an exception occurred. It is useful for closing resources if you're not using try-with-resources.

Example:

BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader("data.txt"));
    System.out.println(reader.readLine());
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (reader != null) reader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
You can also try this code with Online Java Compiler
Run Code

Useful for cleanup, but more verbose than try-with-resources.

In Java, destructors are not needed because memory is automatically managed through garbage collection. While C++ developers must manually destroy objects, Java developers rely on constructs like try-with-resources and finally blocks to manage cleanup. Understanding this difference is crucial for writing efficient, bug-free Java code.

How does Java Destructor work?

When an item is formed, space in a heap is taken up by it. The threads make use of these items. The items become eligible for trash collection if the thread is no longer using them. That object's memory space is now free to be used by newly generated objects. It should be noted that the JRE runs the finalise() method to shut off connections to databases and networks when the garbage collector destroys an object. The use of the destructor and garbage collector, as seen above, is the extent of the developer's interaction with memory management. The primary distinction between the two is this. The object will be destroyed at the precise time specified by the destructor. However, the garbage collector performs the identical task automatically in Java. Both of these memory management strategies have advantages and disadvantages. The main issue, though, is that the developer occasionally need rapid access to memory management.

Advantages of Destructor

If we talk about the advantages of destructors in general, it offers one last opportunity to free up resources that are not in use to liberate RAM that is being taken by unused objects. Examples include deleting dynamic objects, shutting system handles, and using files. It also comes with a decent picture to eliminate those unnecessary items, lowering the possibility of memory leak. The compiler automatically creates a destructor for a programme if the user does not define one on their own. 

Now let us see the advantages of destructor in java specifically. 

  • The destructor eliminates the value that the function produced to free up memory space in the heap.
     
  • The program's destructor is always invoked after it is finished.
     
  • Destructor doesn't accept any arguments and is never overloaded.
     
  • The compiler provides a function for us; we don't need to define one.

Disadvantages of Destructor

Disadvantages of using destructors include:

  • Unpredictable Timing: Destructors may not execute immediately, leading to uncertainty about when resources are actually released.
  • Complexity in Exception Handling: Destructors should not throw exceptions; handling errors during destruction can be complicated.
  • Inefficiency with Managed Resources: Over-reliance on destructors for resource management can lead to inefficiencies, especially in garbage-collected environments.
  • Inheritance Complications: Properly managing destructors in inheritance hierarchies can be challenging, requiring careful design to avoid resource leaks or undefined behavior.
  • Potential for Resource Leaks: If not correctly implemented, destructors can lead to resource leaks, especially in cases of early exits from functions or blocks.
  • Difficulty in Debugging: Issues arising within destructors can be harder to trace and debug due to their automatic invocation by the runtime system.

Java Finalize() Method

You can use Object#finalize in Java as a workaround for destructors. Finalize is a unique method called automatically when an object is no longer used. Although not a destructor, it works very similarly to a java destructor. Finalize is a protected method of the Object class, defined in java.lang package.

To use Finalize like a destructor, we need to call it explicitly to override it; otherwise, it is called entirely at the discretion of the garbage collector. We can only call it once. JVM ignores all the exceptions by the finalize() method except the unchecked exception. Apart from calling it by using the method itself, we can also invoke the method System.runFinalizersOnExit(true).

Syntax

Class Object
{
	protected void finalize()
	{
	//resources to close
	}
}

Implementation

Finally, we can see a basic implementation of a Java Destructor using the finalize method.

Code

  • Java

Java

public void finalize() throws Throwable{
System.out.println("Object is destroyed by the Garbage Collector");
}

public static void main(String[] args) {

A test = new A();
test = null;
System.gc();
}
You can also try this code with Online Java Compiler
Run Code

Output

Output for Java Finalize() Method

Example of Destructor

Implementation

  • Java

Java

class Shoes {

  /* instance variables of shoes class*/
  private String Brand;
  private int Size;

  /* constructor */
  public Shoes(String Brand, int Size) {
      this.Brand = Brand;
      this.Size = Size;
  }

  public static void main(String[] args) {

      /* create an object */
      Shoes myshoes = new Shoes("nike", 10);
    
      /* print the values assigned to the instance variables */
      System.out.print("Brand - ");
      System.out.println(myshoes.Brand);
      System.out.print("Size - ");
      System.out.println(myshoes.Size);

      /* set myshoes reference to null */
      myshoes = null;

      /* request to garbage collector */
      System.gc();

  }

  /* @override the finalize method of 'Object' class */
  protected void finalize() throws Throwable {
      System.out.println("myshoes object is destroyed");
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

Brand - nike
Size - 10
myshoes object is destroyed


We have a 'Shoes' class with two instance variables, "Brand" and "Size." The Constructor of this class takes two parameters. In the 'main()' function, when a 'myshoes' object is created, 'nike' is assigned to the 'Brand' instance variable, and '10' is assigned to the 'Size' instance variable. After that, we set the 'myshoes' reference to null so that it will no longer be accessible by any references in the program. Finally, we call the garbage collector by the 'system.gc()' function. Before the object is garbage collected, the garbage collector calls the 'finalize()' method. The 'finalize()' method is overridden in the 'Shoes' class as it is defined in the 'Object' class, which is a superclass of all other classes in Java. After the execution of the 'finalize()' method, the "my shoes" object will be destroyed, and the garbage collector will proceed with the actual garbage collection process.

Java Destructor vs Constructor: Key Differences

What is a Constructor in Java?

A constructor in Java is a special method used to initialize objects. It shares the class name and has no return type. When an object is created using the new keyword, the constructor is automatically called to set up initial values or execute setup logic. Java supports default, parameterized, and copy constructors. Constructors can be overloaded, and they are essential in object creation and setup.

Example – Constructor Usage:

class Car {
    String model;

    // Constructor
    Car(String model) {
        this.model = model;
        System.out.println("Car created: " + model);
    }

    public static void main(String[] args) {
        Car c = new Car("Honda");
    }
}

Does Java Have Destructors?

Java does not have destructors like C++. In Java, memory is automatically managed by the Garbage Collector. When an object is no longer reachable, the JVM reclaims the memory. Although Java had a finalize() method to perform cleanup tasks before deletion, it is now deprecated due to performance issues and unpredictability. Instead, Java encourages using try-with-resources and finally blocks to release resources like file handles and database connections.

Example – Simulating Destructor Behavior:

class MyResource implements AutoCloseable {
    public void use() {
        System.out.println("Using resource...");
    }

    @Override
    public void close() {
        System.out.println("Resource closed.");
    }

    public static void main(String[] args) {
        try (MyResource r = new MyResource()) {
            r.use();
        }
    }
}

Comparison Table: Java Constructor vs (Simulated) Destructor

AspectConstructorDestructor (Simulated)
PurposeInitializes new objectsCleans up before object is destroyed
InvocationAutomatically on object creationAutomatically during garbage collection
SyntaxSame name as class, no return typefinalize() method (now deprecated)
AvailabilityAvailable in JavaNot truly available (simulated only)
Resource HandlingSets initial stateUse try-with-resources, finally
Lifecycle RoleBegins the object's lifecycleEnds the object's lifecycle

Understanding the difference between constructors and destructors in Java is key for writing efficient and clean code. While constructors help in object creation, Java handles destruction via automatic garbage collection. Developers must use proper resource management practices like try-with-resources to prevent leaks and ensure predictable behavior.

Frequently Asked Questions

What is deconstructor in Java?

Java does not have a deconstructor. Instead, it employs automatic garbage collection to manage memory, eliminating the need for explicit destructors.

Does Java have a deconstructor?

No, Java does not have a deconstructor. Memory management is handled through automatic garbage collection, simplifying resource cleanup.

What is an example of a destructor?

Destructors are not used in Java. In languages like C++, an example is ~MyClass() for cleaning up resources when an object is destroyed.

What is the difference between constructor and destructor?

Constructors initialize objects during creation, while destructors (not present in Java) clean up resources when an object is destroyed or goes out of scope in languages like C++.

What is a destructor method?

A destructor method is a special function in object-oriented programming that is automatically invoked when an object is destroyed or goes out of scope, responsible for releasing resources and performing clean-up tasks for the object.

Conclusion

In this article, We have gone over the concept and implementation of java destructor. Java, with its automatic garbage collection mechanism, eliminates the need for explicit destructors. The absence of destructors simplifies memory management, promoting cleaner and safer code.

Live masterclass