最终课程中的非最终方法 [英] Non-final methods in a final class

查看:133
本文介绍了最终课程中的非最终方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题非常简单:

编译器是否将最终类中的所有方法视为最终的?将 final 关键字添加到最终类中的方法是否有效?

My question is pretty simple:
Does the compiler treat all the methods in a final class as being final themselves? Does adding the final keyword to methods in a final class has any effect?

我知道最终方法有一个更好的内联机会,这就是我要问的原因。

I understood that final methods have a better chance of getting inlined and this is why I am asking.

提前致谢。

推荐答案

你是对的,最终类中的所有方法都是隐含的最终方法。

You're correct, all methods in a final class are implicitly final.

见这里:


请注意,你也可以声明一个完整的类final。一个
声明final的类不能被子类化。这对于
例子来说,在创建一个像String类这样的不可变类时特别有用。

"Note that you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class."

此处:


最终类中的所有方法都是隐式最终的。

All methods in a final class are implicitly final.

您可能也对此感兴趣: Java最终关键字的效果提示

This may also be of interest for you: Performance tips for the Java final keyword

这篇关于最终课程中的非最终方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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