Finalize() Method In Java With Examples - CodeSpeedy
Maybe your like
In this tutorial, we are going to learn about the finalize method in Java with some examples.
finalize() method
Finalize is a method of an object class in which the garbage collector is called before destroying the object.
This method does not have any return type, it performs clean up activities.
The finalize method overrides to dispose of system resources, is also useful to minimize memory leak.
First Example
The code given below will explain the use of finalize method in Java.
When we take a string object and make it null then it will not call the finalize method.
public class finalizeMethod { public static void main(String[] args) { String str1 = new String("CS"); str1 = null; System.gc(); System.out.println("output of main method"); } protected void finalize() { System.out.println("output of finalize method"); } } Output:- output of main methodEXAMPLE 2
But when we take the object of the class and make it null then, first it will call the main method after that it will call the finalize method.
public class finalizeMethod { public static void main(String[] args) { finalizeMethod str2 = new finalizeMethod (); str2 = null; System.gc(); System.out.println("output of main method"); } protected void finalize() { System.out.println("output of finalize method"); } }Output:-
output of main method output of finalize methodAlso read: toString() method in Java
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
« Print all unique words from a string in JavaHow to call a function in Python using Node.js » Search
Related Posts
Tag » What Is Finalize In Java
-
Finalize() Method In Java And How To Override It? - GeeksforGeeks
-
Java Object Finalize() Method With Examples - Javatpoint
-
Finalize() Method In Java - Scaler Topics
-
A Guide To The Finalize Method In Java - Baeldung
-
ng.nalize() Method - Tutorialspoint
-
Java Finalize 함수 - 기술 블로그
-
How To Handle Java Finalization's Memory-Retention Issues - Oracle
-
Finalize In Java | Complete Guide To How Finalize() Method Work In ...
-
When Is The Finalize() Method Called In Java? - Stack Overflow
-
Finalize Method In Java - Gate Vidyalay
-
Finalize - The Java Interview Handbook: 300+ Interview Questions
-
Time To Say Goodbye To The Finalize Method In Java - Medium
-
Finalize Method In Java And Best Practices - JavaGoal
-
[PDF] Java Finalize Method, Orthogonal Persistence And Transactions