Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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)?

What's best to use?

  private Result doSomething()
  private final Result doSomething()
  private static Result doSomething()
  private static final Result doSomething()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
94 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...