In addition to instance methods and class methods, there is another special type of method called a static method.
A static method is not bound to the instance or the class and doesn’t receive any special parameters. A static method can be called on the class itself or on an instance of the class.
The following code implements a class named Student including a static method:
We can see that the static method is defined inside the class, but it doesn’t have access to the instance data or the class data. It can be called on the class itself or on an instance of the class.
Some common uses of static methods include utility functions that perform tasks such as formatting data or validating input, and methods that provide a logical grouping of related functions, but do not need to modify the state of the instance or the class.
Therefore, a good OOP practice is:
Define a function as a static method within a class if this function’s logic is closely related to the class.