Search code examples
javastatic-methods

Should class methods be made static in all cases if they don't use absolutely any class fields?


Should class methods be made static in all cases if they don't use absolutely any class fields?

Are there exceptions? According to my logic, if a class method does not use any of its fields, then it must be static. Right?


Solution

  • If a method doesn't use any instance variables in it's containing class then it doesn't have to be static, but it should be. As a general rule, if a method doesn't require a class instantiation to be used then it should be extracted to a utility class/interface as a static method.