
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the purpose of overriding a finalize() method in Java?
The finalize() method is a pre-defined method in the Object class and it is protected. The purpose of a finalize() method can be overridden for an object to include the cleanup code or to dispose of the system resources that can be done before the object is garbage collected. If we are overriding the finalize() method then it's our responsibility to call the finalize() method explicitly. The finalize() method can be invoked only once by the JVM or any given object.
Syntax
protected void finalize() throws Throwable
Example
public class FinalizeMethodTest { protected void finalize() throws Throwable { try { System.out.println("Calling finalize() method of FinalizeMethodTest class"); } catch(Throwable th) { throw th; } finally { System.out.println("Calling finalize() method of Object class"); super.finalize(); } } public static void main(String[] args) throws Throwable { FinalizeMethodTest test = new FinalizeMethodTest(); String str = "finalize() method in Java"; str = null; System.out.println(str); test.finalize(); } }
Output
null Calling finalize() method of FinalizeMethodTest class Calling finalize() method of Object class
- Related Articles
- What is the purpose of using a dumpStack() method in Java?
- What is the difference between method hiding and method overriding in Java?
- Method overriding in Java
- How many times finalize method will be invoked? who invokes finalize() method in Java?
- Using the finalize() method in Java Garbage Collection
- What is the purpose of using Optional.ifPresentOrElse() method in Java 9?
- What are the rules on method overriding in Java?
- What is the purpose of toString() method? Or What does the toString() method do in java?
- What is the purpose of the flush() method of the BufferedWriter class in java?
- What is the purpose of a constructor in java?
- overriding method different package in java
- When Method Overriding occurs in Java?
- Rules for Java method overriding
- What is the purpose of interfaces in java?
- What is the purpose of a default constructor in Java?

Advertisements