Suggest an editImprove this articleRefine the answer for “What does the static keyword do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The **`static`** keyword makes a property or method belong to the class itself rather than to an instance, so it is accessed through the class name (`MyClass.member`) rather than through a `new`-created object. **Key point:** static members are inherited, can be `private`/`protected`/`readonly`, and inside the class they are accessed through the class name rather than `this`.Shown above the full answer for quick recall.Answer (EN)Image## 1. The main idea Normally, methods and properties are created for a **class instance** (via `new`): ```javascript class User { name: string; constructor(name: string) { this.name = name; } greet() { console.log(`Hello, ${this.name}!`); } } const u = new User("Tim"); u.greet(); // Hello, Tim! ``` But `static` makes a property **accessible only through the class itself**, not through an object: ```javascript class User { static role = "user"; // a static property } console.log(User.role); // accessed through the class // console.log(u.role); // Error - the instance has no role property ``` --- ## 2. Example: static methods ```javascript class MathUtils { static sum(a: number, b: number): number { return a + b; } static multiply(a: number, b: number): number { return a * b; } } console.log(MathUtils.sum(3, 4)); // 7 console.log(MathUtils.multiply(3, 4)); // 12 ``` Such methods can be called **without creating an instance** - they are "global functions" grouped inside a class. --- ## 3. The difference from regular methods | Characteristic | Regular method | `static` method | |---|---|---| | Belongs to | an instance (`new MyClass()`) | the class itself (`MyClass`) | | Accessible from an instance | Yes | No | | Accessible through the class | No | Yes | | Has access to `this` (the instance) | Yes | No (only the class) | --- ## A comparison example ```javascript class Counter { static count = 0; // shared by all instanceCount = 0; // individual constructor() { Counter.count++; this.instanceCount++; } static reset() { Counter.count = 0; } } const c1 = new Counter(); const c2 = new Counter(); console.log(Counter.count); // 2 (shared) console.log(c1.instanceCount); // 1 console.log(c2.instanceCount); // 1 Counter.reset(); console.log(Counter.count); // 0 ``` > `Counter.count` is a field shared by all instances, > while `this.instanceCount` is unique to each object. --- ## 4. Static properties and methods can be typed ```javascript class Config { static readonly API_URL: string = "https://api.example.com"; static version: number = 1.0; static getInfo(): string { return `Version: ${Config.version}`; } } console.log(Config.API_URL); console.log(Config.getInfo()); ``` > Modifiers (`public`, `private`, `readonly`) can be used together with `static`. --- ## 5. `static` + `private` Static members can be **private**, meaning accessible only inside the class itself: ```javascript class Database { private static connection: string; static connect(url: string) { Database.connection = url; console.log(`Connected to ${url}`); } static getConnection() { return Database.connection; } } Database.connect("mongodb://localhost"); console.log(Database.getConnection()); // accessed through the public static // Database.connection; // private ``` --- ## 6. Accessing static members inside the class Inside the class you must access them **through the class name**, not through `this`: ```javascript class Example { static count = 0; increment() { // this.count++; // error - the instance has no count Example.count++; // correct } } ``` > `this` inside a regular method refers to the **instance**, > while static fields belong to the **class**. --- ## 7. Static blocks (ES2022 / TS 4.4+) TypeScript supports **static blocks** - code that runs **once, when the class is loaded**. ```javascript class Settings { static config = {}; static { console.log("Initializing Settings..."); Settings.config = { mode: "prod" }; } } console.log(Settings.config); // { mode: "prod" } ``` > This is convenient for initializing static data that should not live inside the constructor. --- ## 8. Inheritance of static members Static properties and methods **are inherited**: ```javascript class Base { static type = "base"; static describe() { console.log(`Type: ${this.type}`); } } class Child extends Base { static type = "child"; } Base.describe(); // Type: base Child.describe(); // Type: child ``` > `this` in a static method refers to **the class itself**, > not to the parent. --- ## Summary | Capability | Description | |---|---| | `static` | Makes a property/method belong to the class, not to an instance | | Where it's used | Inside classes | | Access | Through the class name (`MyClass.member`) | | Inherited | Yes | | Can be `private` / `protected` / `readonly` | Yes | | Accessible from an instance | No | | Supports `static {}` blocks and initialization | Yes (TS 4.4+) | --- **In simpler terms:** > `static` makes a method or property "shared" across the whole class, > not tied to a specific object. > > It is great for **utilities**, **constants**, **counters**, **factories**, and **initialization**.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.