Class Method vs Static Method
PronunciationKLAS METH-ud vee-ess STAT-ik METH-ud
Definition
A class method receives the class itself and is often used as an alternative constructor. A static method receives neither the instance nor the class; it is just a function that lives inside the class.
Where you hear it
In Python and Java class design, code reviews, and interviews comparing @classmethod with @staticmethod.
Examples
Money.from_string("12.50 EGP")is a class method that builds a Money object.The validation helper is a static method because it does not use the object.
The class method reads cls to build the right subclass, while the static method only does a calculation.
Common mistake
Making a method static when it should be a plain module function. If it needs neither the class nor the instance, ask whether it belongs in the class at all.
Don't confuse with
An instance method, which receives self and works with one specific object.
Say it at work
- In a meeting or chat
Add a class method for creating it from a dictionary.
- In an email or ticket
That static method could just be a function in the module.
Keep exploring
More in Programming Fundamentals