Java Destructors
Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an idea of how it works.
Java Constructors
Constructor is a special type of method. And this method is used for initializing the objects. When we create an object for the class, this constructor will be automatically called. Method and constructors are similar. A constructor can also define as a block of codes which is directly called when the object is created for the class. Creating the object for the class is also called an instance of class creation. At least one constructor is called when we create an object to the class using the new () keyword.
Another thing is in the Java compiler, and there will be a default constructor. This Java compiler only creates this default constructor. This default Java constructor does not have any parameters. In the simplified version, we can say that if in a Java program there is no constructor in a class, the default constructor will be created.
Let's understand this Java constructor concept by creating a class and by creating a default constructor by taking simple example code.
// A simple Java program for understanding the concept of the Creation of object default constructor
Class Java
{
……….
//Creation of constructor
New Java () {
…………..
}
// The below line of code gives the explanation of how we create an object for the above class. And the below line of code will call the above-created constructor automatically.
Java object = new Java ();
Let us look at a simple Java program about how the constructor works to understand it more clearly.
//Class name is Student
class Student{
int ID;
String student_name;
//Creating constructor by passing parameters
Student(int n,String N){
ID = n;
student_name = N;
}
//Display the ID's and Names of the students
void display_info(){System.out.println("The student id is:"+ID);
System. out.println("Name of the student is:"+student_name);
}
public static void main(String args[]){
//Creating instances for class by passing the values
Student s = new Student(111,"Isha");
Student s = new Student(222,"Krishnan");
//Calling method to display for displaying the values
S.display_info();
s.display_info();
}
}
Output:

In the above, we can understand how the constructor is called and how the parameters will be passed.
Java Destructor
The method which is automatically called when the object is no longer needed is known as “Destructor”. Destructor is a special method. Java destructors are non-deterministic. These destructors are also called finalizers. The garbage collector in Java handles the allocation and deallocation of objects.
The object occupies space in the memory when we create an object of the class. And this memory will be deallocated by the destructor. In Java, there is a garbage collector that does the same work which is done by the destructor. Usually, objects occupy unnecessary space if the memory occupied by them does not get deleted. So, to delete this memory and overcome this problem, we use destructors. The garbage collector deletes the object when the object completes its life cycle and releases the memory that is occupied by the object.
Destructor and finalize () methods do the same job. Constructor and destructor both works in the opposite way. Simply, we can say the constructor is the opposite of the destructor. If the constructor is used to initialize the objects, then destructors release the memory occupied by the object.
Syntax for finalize () method:
Protected void finalize throws Throwable ()
{
// resources to be close
}
The thing and the point to understand is the finalize () method is not a destructor but works similar to a destructor.
Java provides a garbage collector. This garbage collector works similar to a destructor. Actually, the thing is, there is no concept of destructors in this Java programming language.
Let's see an example to understand the concept of destructors.
Destructor.java
public class Destructor
{
public static void main(String[] args)
{
Destructor d= new Destructor ();
d.finalize();
d = null;
System.gc();
System.out.println("In the main method");
}
protected void finalize()
{
System.out.println("Garbage collector destroys the object.");
}
}
Output:

Advantages of Destructor
- The resources will be released from occupied by the object.
- Destructor is automatically invoked at the end of the program execution. That is the reason no explicit call is required.
- The destructor cannot be overloaded.