Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to make a method static? [closed]

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?

like image 864
Dónal Avatar asked Apr 26 '10 13:04

Dónal


People also ask

How do you know when to make a method static?

Static methods are usually preferred when: All instance methods should share a specific piece of code (although you could still have an instance method for that). You want to call method without having to create an instance of that class. You must make sure that the utility class is never changed.

Why should I make a method static?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

When should be method static and non-static?

A static method can access only static members and can not access non-static members. A non-static method can access both static as well as non-static members. Static method uses complie time binding or early binding. Non-static method uses run time binding or dynamic binding.

What are the rules for static methods?

A static method can access static methods and variables as follows: A static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class.


2 Answers

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.
like image 75
Thomas Avatar answered Oct 22 '22 03:10

Thomas


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.)

like image 36
Jonik Avatar answered Oct 22 '22 04:10

Jonik