When should i use static functions?

Hi, can anyone explain it to me why and when should i use static-functions? Thx

Static functions enable you to call code without an instance of the class in which it's defined.

You cannot use instance data in a static function.

Normally, a static function should be self-contained. I.e. it should perform its function without requiring or saving any stateful data. A classic static function would be something like this (C#):

// MyClass.cs (C#):
public class MyClass {
    public static float Multiply (float a, float b) {
        return a*b;
    }
}

// MyClass.js (Javascript):
static function Multiply (float a, float b) : float {
    return a*b;
}

You can then call this function directly on MyClass without requiring an instance object of MyClass:

product = MyClass.Multiply(3, 2);

There are some different use cases though, where static functions do alter state that is saved in static variables. One example of this is the singleton programming pattern.

There are no "rules" for when to use static functions. You simply need to understand what it is and how it works, and determine for yourself in each situation if a static function is a good solution for the problem at hand.

Check this link for your complete answer…