Skip to content
BENIBIX Learn

Static Method

Development🧭 Programming ParadigmsObject-Oriented Programming

Also known as Class Method, Static Function, staticmethod

A method that belongs to the class, not to any one object.

Think of it like

It is like a school bell. It belongs to the whole school, not to one child.

Example

A Temp class has a static method that turns 20 degrees C into F. Code calls Temp.toF(20) and never makes an object.

Why it matters

It puts helpers next to the class they belong to. You can use them without making an object first.

Definition

A static method belongs to the class itself, not to one object made from it. You call it on the class name, like Math.max(3, 7). It cannot see any one object's properties, because there is no object. Static methods suit helpers and counters that make sense for the whole class. Java, C# and JavaScript mark them with the word static.

Example code

javascript
class Temp {
  static toF(c) { return c * 9 / 5 + 32; }
}
console.log(Temp.toF(20));
// 68
The method is called on the class name. No object is needed.
count is a static method. It belongs to the Dog class, not to Rex or Bella.

Common mistake

Making every method static so you never make objects. Then you lose the point of objects: data kept with the code that uses it.

Key takeaways

  1. 1
    Class, not object. Called on the class name.
  2. 2
    No object data. It cannot read one object's values.
  3. 3
    Good for helpers. Shared tools and counters.