Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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.
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:
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:
Let’s take a look at a few advantages of garbage collection in Java:
Let’s try to understand why Destructors are not used in Java.
Constructor | Destructor |
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 |
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 14th December,2024 14th December SAT&SUN (Weekend Batch) | View Details |
edureka.co