When should I write Static Methods?

后端 未结 8 831
梦如初夏
梦如初夏 2021-01-04 02:31

So I understand what a static method or field is, I am just wondering when to use them. That is, when writing code what design lends itself to using static methods and field

8条回答
  •  梦谈多话
    2021-01-04 03:05

    I would say use static methods whenever you have functions which are independent of the state of the instance, ie. doesn't depend on any instance fields.

    The less non-local state that a method depends on, the easier it is to understand, so static is a helpful signal to the reader of the code.

提交回复
热议问题