Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Why Java does not have a Destructor?

Last Updated on March 16, 2023 by Prepbytes

There are many programming languages available some of them support object-oriented programming and some of them are not. Some of the languages that supported object-oriented programming languages are C++, Java, Python, etc. So let’s take the example of java, in java we are dealing with classes and objects and when the object is created the memory is allocated to it with the help of a constructor but the memory needs to be free after the work of the object has been completed if not it will need to memory overflow. So in these languages, the memory and other resources are freed with the help of a destructor. We will learn all about java destructor by moving further in this article along with its working, advantages, and example.

What is a Destructor?

The object is initialized with the help of the constructor as the constructor initializes the memory and all the resources to the object but when the work of the object has been done we have to delete or unassing all the space and memory of that object otherwise it will create a problem such as memory overflow, etc. To resolve this problem we use a destructor. But in java, there is no destructor instead of the java destructor we have a garbage collector which does the exact same work as the java destructor and we do not even have to initialize or explicitly call the garbage collector. It will prevent all memory leaks as it does not run out of memory.

Why Java does not have a Destructor?

As explained above that there is no java destructor instead the garbage collector is responsible for freeing up memory. It checks all the objects periodically and searches for the objects that are no longer in use and frees the memory they are using because of this there is no need for a java destructor instead this mechanism in java is called finalization.

Advantages of Destructor

Some of the advantages of java destructor are given below:

  • Finalizations will release any resources that are used by the objects that are no longer in use.
  • It will take care of the cases when the exception is thrown.
  • It will automatically clean the resources there is no need to explicitly call it.
  • It will handle all the errors that occurred during cleanup.
  • It is compatible with all the classes in java.

Working of Java Destructor

Whenever an object is created it occupies some space in the heap. Threads use these objects. If the threads do not require some of the objects then they are eligible for garbage collection. After the memory is freed now the memory captured or used by that object is now free and available to be used by new objects that are being created. The garbage collector will destroy the object and the JRE will call the finalize the method to close the connections like network and database connection.

Till now we have understood that the garbage collector and destructor in java are the same but there is a slight difference between the two in that the destructor notifies whenever it is deleting an object whereas the garbage collector in java deletes the object automatically. Both of them have some benefits and drawbacks but the main reason for using both of them is to get free memory.

What is Finalization in Java?

It is a mechanism that is used in java to clear up an object before it is even garbage collected. When the object is not in use then the garbage collector checks the object if it has finalize method associated with it then it calls the finalize method before freeing up the memory. The only purpose of the finalize method is to call release any resource object is holding before cleaning the object.

How Finalization Works in Java?

For programmers, it can be quite difficult to fully understand garbage collection and implement it. To solve this problem there is an alternate method that java provides which is finalize method(). It works the same as the way the destructor works.

Syntax of finalize in Java

protected void finalize throws Throwable()  
{  // the resources that need to be free

It offers additional protection but is not a destructor. Before shutting down the application, it makes sure that exterior resources have been used, such as closing the file. It can be called directly by using the method or by using the System method. runFinalizersOnExit(true).

Methods of finalize()
Some of the methods of the finalize methods are given below.

  • It is predefined in the java.lang.package and it is a protected method of the object class.
  • We can call it only once.
  • If we want to override the method then we need to explicitly call the finalize () method.
  • The garbage collector executes the gc() method of the JVM. It is called when the heap memory is full and we need more memory for the new objects that are coming.
  • It will ignore all the exceptions other than the unchecked exceptions.

Example of Destructor

In this section, we will discuss the example of a destructor with code and its implementation.

Code Implementation

 class ExampleDestructor  
public static void main(String[] args)  
ExampleDestructor de = new ExampleDestructor ();  
de = null;  
System.out.println("It is inside the main() method");  
protected void finalize()  
System.out.println("The garbage collector destroyed the object");  


The garbage collector destroyed the object
It is inside the main() method
The garbage collector destroyed the object

Explanation of the above example
In the above example we have created a class with name ExampleDestructor and after that, we have created the object of the class and when the object work is complete we are destroying the object using finalize method and garbage collection methods.

Unlike some other programming languages, like C++, Java lacks destructors. When an object is no longer being referenced, Java’s garbage collector deallocates the memory on its own.

The background process known as the garbage collector occasionally releases memory from objects that are no longer in use. Developers don’t have to explicitly create destructors or worry about memory leaks as a result.

Instead, before an object is garbage collected in Java, cleaning operations are carried out using the finalize () function. The use of other techniques, such as try-with-resources or explicit stop methods, is advised for resource cleaning since this method is not assured to be called.

Frequently Asked Questions

Here are some of the frequently asked questions about java destructor.

1. In java is the finalize() method guaranteed to be called?
No there is no guarantee the garbage collector will not call the finalize() function if the object is in use.

2. How can you force the garbage collector to run the finalization process?
Yes you can by calling the System.gc() method.

3. Can you skip the finalization process for an object?
Yes, you can skip the finalization process for an object by calling the System.runFinalizersOnExit(false) method.

4. How can you check if an object has been finalized?
You can check if an object has been finalized by calling the isFinalized() method.

5. Can you resurrect an object during the finalization process?
Yes, you can resurrect an object during the finalization process by calling the object’s finalize() method from within the finalize() method.

Leave a Reply

Your email address will not be published. Required fields are marked *