不可见类型为Object的方法finalize()吗? [英] The method finalize() from the type Object is not visible?

查看:123
本文介绍了不可见类型为Object的方法finalize()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写的类的main方法中尝试了以下代码:

I tried the following code in a main method of a class that I wrote:

public static void main(String[] args){
    ...
    Object s = new Object();
    s.finalize();
    ...
}

但是,日食给我一个提示

However, the eclipse give me a tip that

The method finalize() from the type Object is not visible

我很困惑,因为Object类型具有受保护的finalized方法,该方法应该可以自己看到?反正我错了吗?

I am so confused because the type Object has a protected finalized method, which is supposed to be visible by its own? Am I wrong anyway?

推荐答案

Object#finalize() is a protected method. You can't call it like that. A protected member of a class is inherited by it's direct subclass. You can access it inside that direct subclass on this reference, but not using the reference of that class directly.

是这样的:

class Demo {
    public void test() { 
        this.finalize();
    } 
}

顺便说一句,为什么要调用它?在将对象从内存中完全删除之前,JVM会自动调用该方法以清除对象正在使用的所有资源.

BTW, why do you want to invoke it? That method is automatically invoked by JVM to clear any resources that an object is using, just before the object is completely removed from the memory.

这篇关于不可见类型为Object的方法finalize()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆