Skip to main content

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 a TypeError.
  • Inside another static method this.otherStatic() works, because this is the class.
  • From an instance method a static is reachable only through the class name: User.hello().
  • extends inherits both regular and static methods, so Dog.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

javascript
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 function

The 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:

javascript
class MathUtils { static sum(a, b) { return a + b; } } console.log(MathUtils.sum(3, 5)); // 8

Here 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 this inside other static methods,
  • but not through this from an instance.
javascript
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:

javascript
class Animal { static info() { console.log('This is an animal'); } } class Dog extends Animal {} Dog.info(); // 'This is an animal'

extends inherits both regular and static methods.

When a subclass overrides a static, the parent implementation is reached with super:

javascript
class Animal { static describe() { console.log('Animal'); } } class Dog extends Animal { static describe() { super.describe(); // calls Animal.describe() console.log('Dog'); } } Dog.describe(); // Animal // Dog

A real world example: the factory method

javascript
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 itExampleWorks?
On the classUser.sayHi()Yes
On an instancenew User().sayHi()No
Inside another staticthis.otherStatic()Yes
Inside a regular methodClassName.staticMethod()Yes
In a subclass through supersuper.staticMethod()Yes

Common mistakes

  • Calling a static on an instance. new User().sayHi() throws TypeError: u.sayHi is not a function.
  • Writing this.staticMethod() in an instance method. There this is the instance, which has no statics. Use ClassName.staticMethod() or this.constructor.staticMethod().
  • Detaching the method from its class. const f = Counter.increment; f(); loses this, so you need bind or an arrow wrapper.
  • Forgetting super in 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.