Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the Destructor in Java?
3.
How does Java Destructor work?
4.
Advantages of Destructor
5.
Disadvantages of Destructor
6.
Java Finalize() Method
6.1.
Syntax
6.2.
Implementation
6.3.
Code
6.4.
Java
6.5.
Output
7.
Example of Destructor
7.1.
Implementation
7.2.
Java
7.3.
Output
8.
Frequently Asked Questions
8.1.
What is deconstructor in Java?
8.2.
Does Java have a deconstructor?
8.3.
What is an example of a destructor?
8.4.
What is the difference between constructor and destructor?
8.5.
What is a destructor method?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Java Destructor

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.

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.

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.

If you find any problems, please comment. We will love to answer your questions. Also, Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Live masterclass