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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值