Home » Java programming language

Java Object Class protected void finalize() throws Throwable method with Example

Java Object class protected void finalize() throws Throwable method: Here, we are going to learn about the protected void finalize() throws Throwable method of Object class with its syntax and example.
Submitted by Preeti Jain, on June 28, 2019

Object Class protected void finalize() throws Throwable

  • This method is available in java.lang.Object.finalize().
  • This method is called by the garbage collector when no more references remain.
  • This method is useful for cleanup activities.
  • This method is overridable by only child classes because the method is protected.

Syntax:

    protected void finalize() throws Throwable{
    }

Parameter(s):

Here we don't pass any parameter in the method of the Object class.

Return value:

The return type of this method is void that means this method returns nothing after execution.

Java program to demonstrate example of Object Class finalize() method

public class FinalizeClass {
    public static void main(String[] args) {
        String str = new String("Hi, Welcome in Java World");
        str = null;

        // JVM can call gc() method anytime because str hold null
        System.gc();
        System.out.println("We are in finalize class");
    }

    // Here we are overriding finalize method 
    public void finalize() {
        System.out.println("hi, We are in finalize() method ");
    }
}

Output

D:\Programs>javac FinalizeClass.java

D:\Programs>java FinalizeClass
We are in finalize class



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.