finalize java_受保护的Java对象类void finalize()抛出带有示例的Throwable方法

finalize java

受保护的对象类void finalize()引发Throwable (Object Class protected void finalize() throws Throwable)

  • This method is available in java.lang.Object.finalize().

    此方法在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.

在这里,我们没有在Object类的方法中传递任何参数。

Return value:

返回值:

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

该方法的返回类型为void ,这意味着该方法在执行后不返回任何内容。

Java程序演示对象类的finalize()方法的示例 (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


翻译自: https://www.includehelp.com/java/object-class-protected-void-finalize-throws-throwable-method-with-example.aspx

finalize java

你可能感兴趣的:(finalize java_受保护的Java对象类void finalize()抛出带有示例的Throwable方法)