Accessing Static Methods
You'll learn how to call static methods the correct way—through the class name, not through instances.
The Core Idea
Static methods belong to the class itself, not to individual instances. This means you must call them using the class name as a prefix, like ClassName.methodName(). You cannot call them using this inside other instance methods, and you cannot call them on an instance variable (like myInstance.staticMethod()—that won't work).
Think of static methods like tools hanging on the wall of a workshop. Everyone uses the same tools by going to the wall (the class), not by having their own personal copy. You don't ask a carpenter for their hammer; you go to the tool wall where the hammer lives.
How to Access Static Methods
class MathHelper {
static double(num) {
return num * 2;
}
}
// ✅ Correct: Use the class name
console.log(MathHelper.double(5)); // 10
// ❌ Wrong: Cannot call on an instance
const helper = new MathHelper();
console.log(helper.double(5)); // TypeError: helper.double is not a function
Inside the class, you still use the class name:
class Calculator {
static add(a, b) {
return a + b;
}
static addAndDouble(a, b) {
// Use Calculator, not 'this'
return Calculator.add(a, b) * 2;
}
}
console.log(Calculator.addAndDouble(3, 4)); // 14
Key Takeaway: Static methods are accessed through the class name (like ClassName.method()), never through instances or this in instance methods. They're class-level utilities, not instance-level behaviors.