Skip to main content

What does the static keyword do?

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

CharacteristicRegular methodstatic method
Belongs toan instance (new MyClass())the class itself (MyClass)
Accessible from an instanceYesNo
Accessible through the classNoYes
Has access to this (the instance)YesNo (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

CapabilityDescription
staticMakes a property/method belong to the class, not to an instance
Where it's usedInside classes
AccessThrough the class name (MyClass.member)
InheritedYes
Can be private / protected / readonlyYes
Accessible from an instanceNo
Supports static {} blocks and initializationYes (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.

Short Answer

Interview ready
Premium

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