In what situations is static method a good practice?

后端 未结 11 1002
走了就别回头了
走了就别回头了 2020-12-04 10:04

I have read the following discussions:

Should private helper methods be static if they can be static , and
Should all methods be static if their class has no mem

11条回答
  •  有刺的猬
    2020-12-04 10:43

    Util classes can be OK to be static. As in someone elses example above, with escaping strings. The problem lies when those utils classes perform functions that we would want to mock. For example, the FileUtils class in apache commons - it is often the case that I want to mock file interactions without having to play with real files. If this was an instance class, it would be easy.

提交回复
热议问题