Suggest an editImprove this articleRefine the answer for “How to call a static method”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**You call a static method on the class itself: `ClassName.methodName()`, because it belongs to the class, not to objects created with `new`.** It is not reachable from an instance (`u.sayHi()` throws a `TypeError`), inside another static method it is reachable through `this`, and a subclass can reach the parent's one with `super.methodName()`. ```javascript class MathUtils { static sum(a, b) { return a + b; } } MathUtils.sum(3, 5); // 8 ``` **Key point:** call statics through the class name; `this` inside a `static` method points at the class, not at an instance.Shown above the full answer for quick recall.Answer (EN)Image**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 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()` 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.