Search code examples
language-agnosticpublic-method

When should you use public static methods


There are a lot of people out there against the use of "public/private" static methods. I have search around,with no luck, and tried to find anyone that advocates good use of static methods.

Assuming the methods will always be Cohesive where are the acceptable areas to use public static methods? Do these methods vary between Java and .NET (aka is it more acceptable in one then the other)?

This recent SO post spurred my anger/interest in this topic.


Solution

  • A static method generally shouldn't:

    • Access any state outside its parameters (because that results in hard-to-change coupling)
    • Modify its parameters (because if it does, why isn't it an instance method of that parameter?)

    Conversely, pure functions (i.e. no side effects) make for good static methods.

    Of course, this should not be taken as absolute dogma.