Javatpoint Logo
Javatpoint Logo

Java Destructor

In Java, when we create an object of the class it occupies some space in the memory (heap). If we do not delete these objects, it remains in the memory and occupies unnecessary space that is not upright from the aspect of programming. To resolve this problem, we use the destructor. In this section, we will discuss the alternate option to the destructor in Java. Also, we will also learn how to use the finalize() method as a destructor.

The destructor is the opposite of the constructor. The constructor is used to initialize objects while the destructor is used to delete or destroy the object that releases the resource occupied by the object.

Remember that there is no concept of destructor in Java. In place of the destructor, Java provides the garbage collector that works the same as the destructor. The garbage collector is a program (thread) that runs on the JVM. It automatically deletes the unused objects (objects that are no longer used) and free-up the memory. The programmer has no need to manage memory, manually. It can be error-prone, vulnerable, and may lead to a memory leak.

What is the destructor in Java?

It is a special method that automatically gets called when an object is no longer used. When an object completes its life-cycle the garbage collector deletes that object and deallocates or releases the memory occupied by the object.

It is also known as finalizers that are non-deterministic. In Java, the allocation and deallocation of objects handled by the garbage collector. The invocation of finalizers is not guaranteed because it invokes implicitly.

Advantages of Destructor

  • It releases the resources occupied by the object.
  • No explicit call is required, it is automatically invoked at the end of the program execution.
  • It does not accept any parameter and cannot be overloaded.

How does destructor work?

When the object is created it occupies the space in the heap. These objects are used by the threads. If the objects are no longer is used by the thread it becomes eligible for the garbage collection. The memory occupied by that object is now available for new objects that are being created. It is noted that when the garbage collector destroys the object, the JRE calls the finalize() method to close the connections such as database and network connection.

From the above, we can conclude that using the destructor and garbage collector is the level of developer's interference to memory management. It is the main difference between the two. The destructor notifies exactly when the object will be destroyed. While in Java the garbage collector does the same work automatically. These two approaches to memory management have positive and negative effects. But the main issue is that sometimes the developer needs immediate access to memory management.

Java finalize() Method

It is difficult for the programmer to forcefully execute the garbage collector to destroy the object. But Java provides an alternative way to do the same. The Java Object class provides the finalize() method that works the same as the destructor. The syntax of the finalize() method is as follows:

Syntax:

It is not a destructor but it provides extra security. It ensures the use of external resources like closing the file, etc. before shutting down the program. We can call it by using the method itself or invoking the method System.runFinalizersOnExit(true).

  • It is a protected method of the Object class that is defined in the java.lang package.
  • It can be called only once.
  • We need to call the finalize() method explicitly if we want to override the method.
  • The gc() is a method of JVM executed by the Garbage Collector. It invokes when the heap memory is full and requires more memory for new arriving objects.
  • Except for the unchecked exceptions, the JVM ignores all the exceptions that occur by the finalize() method.

Example of Destructor

DestructorExample.java

Output:

Object is destroyed by the Garbage Collector 
Inside the main() method
Object is destroyed by the Garbage Collector 






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA