Java/J2EE and SOA (293 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (292 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is the use of Destructor in Java?

Published on Sep 30,2019 192 Views
6 / 71 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

 While working with classes in Java, constructors are used for initializing the instance of a class. The memory is allocated for the object using the constructor but after the object life-cycle is finished and the object is no longer in use, the memory has to be deallocated and released. This is where destructor in Java comes into the picture. In this article, we will learn about destructors in Java, in the following order:

What is a Destructor?

A destructor is a special method that gets called automatically as soon as the life-cycle of an object is finished. A destructor is called to de-allocate and free memory. The following tasks get executed when a destructor is called.

  • Releasing the release locks
  • Closing all the database connections or files
  • Releasing all the network resources
  • Other Housekeeping tasks
  • Recovering the heap space allocated during the lifetime of an object

Destructors in Java also known as finalizers are non-deterministic. The allocation and release of memory are implicitly handled by the garbage collector in Java.

Finalizers in Java have to be implicitly invoked since their invocation is not guaranteed, unlike C# finalizers which get invoked during the .NET run-time.

Let’s take a look at key properties of a destructor:

  • Overloading or inheritance is not allowed
  • No specification of access modifiers or parameters
  • Automatic invocation and no explicit call from the user
  • Used in classes but not in structures
  • The order of the class varies from the most derived class to the least derived class
  • Also called when the object instance is no longer eligible for access
  • Used for releasing un-managed resources instead of managed resources held by the object

Garbage Collector

A garbage collector is a program that runs on the Java virtual machine to recover the memory by deleting the objects which are no longer in use or have finished their life-cycle. An object is said to be eligible for garbage collection if and only if the object is unreachable.

Let’s try to understand how garbage collection works in Java:

Garbage collection is mainly the process of marking or identifying the unreachable objects and deleting them to free the memory. The implementation lives in the JVM, the only requirement is that it should meet the JVM specifications. These are the different types of garbage collectors in Java:

  • Serial Garbage Collector
  • Parallel/Throughput Garbage Collector
  • CMS Collector
  • G1 Collector

Let’s take a look at a few advantages of garbage collection in Java:

  • It automatically deletes the unused objects that are unreachable to free up the memory
  • Garbage collection makes Java memory efficient
  • It need not be explicitly called since the implementation lives in the JVM
  • Garbage collection has become an important and standard component of many programming languages

Let’s try to understand why Destructors are not used in Java.

Constructor vs Destructor: Difference Between A Constructor And A Destructor

ConstructorDestructor

A constructor is used to initialize an instance of a class

A destructor is used to delete or destroy the objects when they are no longer in use

Constructors are called when an instance of a class is created

Destructors are called when an object is destroyed or released

Memory allocation

Releases the memory

Overloading is possible

Overloading is not allowed

They are allowed to have arguments

No arguments can be passed in a destructor

Java Finalize() Method

It becomes fairly difficult for any developer to force the execution of a garbage collector, but there is an alternative to this. We can use the object.finalize() method which works exactly like a destructor in Java.

An Object.finalize() method is inherited in all Java objects. It is not a destructor but is used to make sure or provide additional security to ensure the use of external resources like closing the file, etc before the program shuts down. You can call it by using the method itself or system.runFinalizersOnExit(true).

The use of finalize() method is highly not recommended since it can be very unsafe and in some cases used incorrectly.

Let’s take a simple example to show how finalize() can be used to call the garbage collector.

public class A {
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();
}
}

This brings us to the end of this article where we have learned about the Destructor in Java. I hope you are clear with all that has been shared with you in this tutorial.

If you found this article on “Destructor In Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Destructor In Java” and our team will be glad to answer.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.