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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is the use of Destructor in Java?

Last updated on Jun 17,2021 46.7K Views

6 / 72 Blog from Java Core Concepts

 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’s 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.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 14th December,2024

14th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is the use of Destructor in Java?

edureka.co