PMD规则之Finalizer Rules

本文探讨了Java中finalize()方法的使用规范及注意事项,包括为何不应重载该方法、如何正确实现finalize()以确保资源回收,以及为什么应避免直接调用Object.finalize()。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

·  EmptyFinalizer: If the finalize() method is empty, then it does not need to exist.

翻译  空的finalize():如果finalize()方法是空的,那么它就不需要存在。

·  FinalizeOnlyCallsSuperFinalize: If the finalize() is implemented, it should do something besides just calling super.finalize().

翻译  finalize方法调用父类finalize:如果finalize()被实现,它应该除了调用super.finalize()之外还应该做点别的。

·  FinalizeOverloaded: Methods named finalize() should not have parameters. It is confusing and probably a bug to overload finalize(). It will not be called by the VM.

翻译  finalize重载:方法名是finalize()的方法应该具有参数。因为不带参数容易令人迷惑且可能是一个bug,那样就不会被JVM调用。

·  FinalizeDoesNotCallSuperFinalize: If the finalize() is implemented, its last action should be to call super.finalize.

翻译  finalize没有调用父类的finalize:如果finalize()方法被重新实现,它最后一个动作应该是调用super.finalize();

·  FinalizeShouldBeProtected: If you override finalize(), make it protected. If you make it public, other classes may call it.

翻译  finalize应该是受保护的:如果你覆盖finalize(),使他是protected的,如果作为public的,其它类就可以调用了。

·  AvoidCallingFinalize: Object.finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

翻译  避免调用finalize:Object.finalize()是由垃圾收集器发现没有引用指向这个对象的时候调用的,应尽量避免人为调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值