Should Java methods be static by default?

后端 未结 23 1241
既然无缘
既然无缘 2020-12-04 09:24

Say you\'re writing method foo() in class A. foo doesn\'t ever access any of A\'s state. You know nothing else about what foo does, or how it behaves. It cou

23条回答
  •  無奈伤痛
    2020-12-04 09:34

    Don't use static if you can avoid it. It clashes with inheritance ( overriding ).

    Also, not asked but slightly related, don't make utility methods public.

    As for the rest, I agree with Matt b. If you have a load of potentially static methods, which don't use state, just put them in a private class, or possibly protected or package protected class.

提交回复
热议问题