1

Sometimes, when I'm programming, I have to decide between adding a static method to a class that accepts an instance (or more) of that class or adding a non-static method. Here's an example:

Static

public class Integer {
    // ...
    public static Integer add(Integer a, Integer b) {
        // ...
    }
    // ...
}

Non-static

public class Integer {
    // ...
    public Integer add(Integer addTo) {
        // ...
    }
    // ...
}

In this case the most logical choice would be the first (We think of it as "Take two integers and add them" rather than "Add another integer to an integer, and give us the result"), but sometimes it's not as obvious (for unary operations, for example).

Should non-static functions be reserved only for function which actually modify the state of the object?

Does it depend on the language? For example, in Java, it wouldn't make a difference once compiled, because all objects are actually pointers, but in C-based languages, the first case would pass copies of the object, while the second would pass a pointer and a copy (which suggests that we want to modify it). Also, most programming languages do not allow virtual static methods, so that would probably also make a difference (if you want the method to be overridable, for example).

closed as too broad by gnat, durron597, Ixrec, user22815, GlenH7 Aug 30 '15 at 16:21

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    This topic has been discussed extensively on Stackoverflow, see here, or here I heavily recommend Jon Skeet's answer at the first link. – Doc Brown Aug 29 '15 at 8:36
  • @DocBrown wow, thanks for that link. I have tried to explain this to others before with varying degrees of success. Jon Skeet explains it thoroughly and succinctly as usual. – user22815 Aug 29 '15 at 23:01
2

I program OO mostly in C#, which shares a lot of syntax with Java, so I'll speak from that perspective.

For anything that is a "public API" I use a non-static method. Rarely do I use static methods. Why?

  1. If it's virtual/not-final I can override the behavior in a subclass. In the industry I work in we do a lot of customizations for our customers so this is pretty much the easiest way to override the functionality of our vanilla software suite - make things virtual/not-final and change the stock behavior in a custom overlay.

  2. By being a method on a dependent class/service (even if it's singleton) I can mock the non-static method I'm calling, allowing for unit testing and separation of concerns.

  3. By being a method on a class I can use the decorator pattern to intercept the calls.

There are probably more benefits as well.

I only use static methods if the IDE generates it as such when I'm refactoring the inside of a class so that I'm not repeating myself. I also use it when I'm consuming an API that requires me to do so (i.e. writing a Main method or the like).

Another good case for it may be general-purpose numerical mathematical methods and utilities, like in your add example.