What is the use of Destructor in Java?

Swatee Chand
Edureka
Published in
5 min readSep 30, 2019

--

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:

  1. What is Destructor?
  • Garbage Collector

2. Difference Between Constructor and Destructor

3. Java finalize() Method

4. Example

What is 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 unmanaged 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:

  • 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

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.

This brings us to the end of our blog on Advanced Java Tutorial. I hope you found this blog informative and added value to your knowledge.
If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

34. Java Collections Interview Questions and Answers

35. How to Handle Deadlock in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. What is the concept of String Pool in Java?

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on September 30, 2019.

--

--