In Java, is there any disadvantage to static methods on a class?

后端 未结 14 1419
无人及你
无人及你 2020-12-03 06:37

Lets assume that a rule (or rule of thumb, anyway), has been imposed in my coding environment that any method on a class that doesn\'t use, modify, or otherwise need any ins

相关标签:
14条回答
  • 2020-12-03 07:24

    No there is no disadvantages, rather when you are not accessing any instance members in the method then there is no meaning of having it as an instance method. It is good programming skill to have it as a static method.

    and adding to that you don't have to create any instances to access these methods and thus saving a memory and garbage collecting time.

    0 讨论(0)
  • 2020-12-03 07:26

    No, actually the reason for that advice is that it provides a performance advantage. Static methods can be called with less overhead so any method that doesn't need a reference to this ought to be made static.

    0 讨论(0)
提交回复
热议问题