Are Java static calls more or less expensive than non-static calls?

前端 未结 12 2406
夕颜
夕颜 2020-11-27 11:57

Is there any performance benefit one way or another? Is it compiler/VM specific? I am using Hotspot.

12条回答
  •  心在旅途
    2020-11-27 12:38

    As Jon notes, static methods can't be overridden, so simply invoking a static method may be -- on a sufficiently naive Java runtime -- faster than invoking an instance method.

    But then, even assuming you're at the point where you care about messing up your design to save a few nanoseconds, that just brings up another question: will you need method overriding yourself? If you change your code around to make an instance method into a static method to save a nanosecond here and there, and then turn around and implement your own dispatcher on top of that, yours is almost certainly going to be less efficient than the one built into your Java runtime already.

提交回复
热议问题