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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.