How to call a static method
Static methods are called on the class itself, not on its instances: ClassName.methodName(). The reason is simple: static puts the method on the class, so objects created with new simply do not have it.
Theory
TL;DR
- Correct:
User.sayHi(). Wrong:new User().sayHi(), that is aTypeError. - Inside another static method
this.otherStatic()works, becausethisis the class. - From an instance method a static is reachable only through the class name:
User.hello(). extendsinherits both regular and static methods, soDog.info()works.- A subclass can call the parent implementation with
super.describe(). - The classic use case is a factory method such as
User.createGuest().
Quick example
class User {
static sayHi() {
console.log('Hi from the User class!');
}
}
// Correct:
User.sayHi(); // Hi from the User class!
// Wrong:
const u = new User();
u.sayHi(); // TypeError: u.sayHi is not a functionThe reason: static methods belong to the class itself, not to the objects created with new.
Call syntax and parameters
A static method takes arguments just like a regular one, it is simply called on the class:
class MathUtils {
static sum(a, b) {
return a + b;
}
}
console.log(MathUtils.sum(3, 5)); // 8Here
MathUtils.sum()is called directly through the class name, no instance required.
Calling a static method from inside the class
You can call a static method:
- through the class name (
User.someMethod()), - through
thisinside other static methods, - but not through
thisfrom an instance.
class User {
static hello() {
console.log('Hi!');
}
static greetAll() {
this.hello(); // works, because this = the class itself
}
sayHi() {
// You cannot call the static directly from here:
// this.hello(); // TypeError
User.hello(); // but this works, through the class name
}
}
const u = new User();
User.greetAll(); // Hi!
u.sayHi(); // Hi!Instead of hard coding User.hello() inside an instance method, people often write this.constructor.hello(), which lets a subclass override the static.
Inheritance and calling through super
If a class extends another one, it also gets access to the parent's static methods:
class Animal {
static info() {
console.log('This is an animal');
}
}
class Dog extends Animal {}
Dog.info(); // 'This is an animal'
extendsinherits both regular and static methods.
When a subclass overrides a static, the parent implementation is reached with super:
class Animal {
static describe() {
console.log('Animal');
}
}
class Dog extends Animal {
static describe() {
super.describe(); // calls Animal.describe()
console.log('Dog');
}
}
Dog.describe();
// Animal
// DogA real world example: the factory method
class User {
constructor(name, role) {
this.name = name;
this.role = role;
}
static createGuest() {
return new User('Guest', 'guest');
}
}
const guest = User.createGuest(); // a static method call
console.log(guest); // { name: 'Guest', role: 'guest' }Here User.createGuest() returns a new instance, yet the method itself belongs to the class, not to an object.
A summary of where a static method is visible from:
| Where you call it | Example | Works? |
|---|---|---|
| On the class | User.sayHi() | Yes |
| On an instance | new User().sayHi() | No |
Inside another static | this.otherStatic() | Yes |
| Inside a regular method | ClassName.staticMethod() | Yes |
In a subclass through super | super.staticMethod() | Yes |
Common mistakes
- Calling a static on an instance.
new User().sayHi()throwsTypeError: u.sayHi is not a function. - Writing
this.staticMethod()in an instance method. Therethisis the instance, which has no statics. UseClassName.staticMethod()orthis.constructor.staticMethod(). - Detaching the method from its class.
const f = Counter.increment; f();losesthis, so you needbindor an arrow wrapper. - Forgetting
superin an overridden static. Without it the parent logic simply never runs. - Assuming
super.method()works anywhere. It is available only inside the subclass's own methods, not in an arbitrary function.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.