- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How many times finalize method will be invoked? who invokes finalize() method in Java?
The finalize() method belongs to the Object class. Right before closing an object, the garbage collector makes sure that there are no more references to it and, calls the finalize() method on it.
Therefore, once you override the finalize() method in it, you can do all the cleanup activity like closing the resources like database connection, network connection, etc.
protected void finalize throws Throwable{}
It is invoked only once during the execution of a program.
Following are some notable points about the finalize method.
Since this method belongs the Object class, which is the super class of all the classes in java, you can override it from any class.
This is an empty method but you can override it by writing the code to perform require clean up activity.
It is recommended to use try catch around the cleanup code (closing connections etc.) written in the finalize() method.
It is allowed to call the finalize() method explicitly. It just gets executed like any other method.
When you call the finalize() method explicitly, if the garbage collector is currently executing it an unchecked exception will be raised.
In the same way When you call the finalize() method explicitly and in the middle of the execution when the garbage collector tries to call it an unchecked exception will be raised.
Example
public class FinalizeExample{ public void finalize(){ System.out.println("finalize method is executed..."); } public static void main(String args[]){ FinalizeExample obj = new FinalizeExample(); obj.finalize(); System.gc(); } }
Output
finalize method is executed...
- Related Articles
- Using the finalize() method in Java Garbage Collection
- What is the purpose of overriding a finalize() method in Java?
- final, finally and finalize in Java
- Difference Between Final, Finally and Finalize in Java
- final, finally and finalize in C#
- Difference Between dispose() and finalize() in C#
- What is the difference between final, finally and finalize() in Java?
- What is the difference between Finalize and Dispose in C#?
- How many ways to prevent method overriding in Java?
- Why main() method must be static in java?
- Collections.replaceAll() method and List.replaceAll() method in Java
- Which method must be implemented by all threads in Java?
- Why can't static method be abstract in Java?
- Guidelines to be followed while implementing equals method in Java?
- How to execute a particular test method multiple times (say 5 times) in\nTestNG?
