让私人方法最终? [英] Make private methods final?

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

问题描述

让私人方法最终有用吗?这会改善性能吗?

Is it beneficial to make private methods final? Would that improve performance?

我认为私人决赛没有多大意义,因为私有方法无法被覆盖。所以方法查找应该是有效的,就像使用final一样。

I think "private final" doesn't make much sense, because a private method cannot be overridden. So the method lookup should be efficient as when using final.

并且最好使私有辅助方法静态(如果可能)?

And would it be better to make a private helper method static (when possible)?

什么是最好用的?

  private Result doSomething()
  private final Result doSomething()
  private static Result doSomething()
  private static final Result doSomething()


推荐答案

向方法添加 final 并不能提高Sun HotSpot的性能。如果可以添加 final ,HotSpot会注意到该方法永远不会被覆盖,所以对待它也是如此。

Adding final to methods does not improve performance with Sun HotSpot. Where final could be added, HotSpot will notice that the method is never overridden and so treat it the same.

在Java private 方法是非虚拟的。您无法覆盖它们,甚至可以使用嵌套类,它们可以被子类访问。例如,instructoin调用privates的方法与用于非私有的方法不同。将 final 添加到私有方法没有任何可能性。

In Java private methods are non-virtual. You can't override them, even using nested classes where they may be accessible to subclasses. For instance methods the instructoin to call privates is different from that used for non-privates. Adding final to private methods makes no odds.

与以往一样,这些微观优化不值得花钱时间。

As ever, these sort of micro-optimisations are not worth spending time on.

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

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