58

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. So let's say we have a method that does not access instance fields, do you always define such a method as static, or only if you need to call it statically (without a reference to an instance).

Perhaps another way of asking the same question is whether you use static or non-static as the default?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 2
    This may be of use to you: http://stackoverflow.com/questions/202560/when-should-i-write-static-methods – Christopher Parker Apr 26 '10 at 13:09
  • 1
    Or either of these: http://stackoverflow.com/questions/1375227/should-i-make-my-private-class-methods-static, http://bytes.com/topic/c-sharp/answers/483622-when-does-make-sense-use-static-methods (features an answer by Jon Skeet) – Christopher Parker Apr 26 '10 at 13:16
  • 1
    I think this question cannot really be answered language-agnostic. – extraneon Apr 26 '10 at 13:34

8 Answers8

64

I use static methods whenever I can. Advantages:

  • When calling a static method from inside an instance method, you can be sure that there are no side-effects on the state of the current object.
  • From inside a static method, you can be sure you don't accidentally modify any state of the object instance.
  • You can use a static method from outside the class without constructing an instance. If it was possible to make the method static, it clearly doesn't need an instance, so there's no need to require one.
  • Static methods may be slightly more efficient because no "this" pointer needs to be passed, and no dynamic dispatch is necessary.
Thomas
  • 174,939
  • 50
  • 355
  • 478
30

Kevin Bourrillion wrote an insightful answer on this topic some time ago (admittedly from a Java perspective, but I think it's applicable to other languages too).

He argues that you should basically only use static methods for pure functions.

A "pure function" is any method which does not modify any state and whose result depends on nothing but the parameters provided to it. So, for example, any function that performs I/O (directly or indirectly) is not a pure function, but Math.sqrt(), of course, is.

I tend to agree. (Although in my own code, traditionally, I've probably used way too many static helper methods all over the place... :-P And this surely has made code that uses those methods harder to test.)

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
8

If what the method does depend solely on its arguments, you can make it static. If the method does not instantiate any other of your user defined classes, you can make it static. The default, though, is to have it as non-static.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
8

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.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 3
    Utility functions are also usually static too. Good answer. – NG. Apr 26 '10 at 13:13
  • 1
    This answer is bad because it gives no reasoning other than stating the obvious by repeating the most basic definition of a static class method. – Austin Salgat Feb 12 '16 at 16:51
  • if you will make Math class instance and non staticc method, it will operate on instances. So really it is just how you make the method. So that does not anwer which one should you choose. – Darius.V Jun 27 '23 at 06:40
3

It depends. In languages where non-member functions are possible I'd say that most of the time, if the method could be made static, it should be made a non-member function instead, non-friend if possible. You can tell I have a mostly C++ background.

In "pure" OO languages where non-member functions are not possible it would depend on whether the method is only "incidentally" static (i.e. it just happens not to need access to instance members), or is truly logically static - it is a method of the whole class instead of for a particular instance.

Joris Timmermans
  • 10,814
  • 2
  • 49
  • 75
0

Non static by default, static when I need the functionality to be available from at least two different classes, and I don't want to waste a constructor.

ps. Archimedes rules!

David Rutten
  • 4,716
  • 6
  • 43
  • 72
0

(C#) By default, I use static methods in static classes and non-static methods in non-static classes.

As I elaborate a class, I find myself naturally converging on making it entirely static or entirely non-static. Practially speaking, if I start wanting to define static members within a non-static class, I often find that it will eventually make the most sense to break those out into a separate static class -- either a utility class like Math or a global application class (like .NET's ConfigurationManager).

From an object-oriented perspective, a method is doing something to/with an object. So if you're using an instantiated object, it makes the most sense to me to think of that object's methods as non-static. Technically, you technically can make a non-static class have static members if they don't require access to an instance. But ostensibly, at least, a class's methods would still be doing something to/with that class, so I would still make them non-static. All things being equal, that is.

Calvin Fisher
  • 4,653
  • 5
  • 36
  • 47
0

in context of python - staticmethod are basically a normal function, we keep in the class only because of some logical reasons. classmethod takes 'class' as a first argument, default method takes instance aka self as a first argument but staticmethod does not takes any any argument.

Raju Singh
  • 455
  • 1
  • 6
  • 15