When to make a method static?

后端 未结 7 522
忘掉有多难
忘掉有多难 2021-01-30 02:04

I\'d like to know how people decide whether to define a method as static. I\'m aware that a method can only be defined as static if it doesn\'t require access to instance fields

7条回答
  •  你的背包
    2021-01-30 02:51

    Use static methods when you are performing operations that do not operate on instances of the class.

    A perfect example would be a sqrt method of a Math class.

提交回复
热议问题