When to use static methods

前端 未结 21 1915
旧巷少年郎
旧巷少年郎 2020-11-21 05:05

I am wondering when to use static methods? Say if I have a class with a few getters and setters, a method or two, and I want those methods only to be invokable on an instanc

21条回答
  •  囚心锁ツ
    2020-11-21 05:30

    Static methods are not associated with an instance, so they can not access any non-static fields in the class.

    You would use a static method if the method does not use any fields (or only static fields) of a class.

    If any non-static fields of a class are used you must use a non-static method.

提交回复
热议问题