What does the static keyword do?
1. The main idea
Normally, methods and properties are created for a class instance (via new):
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:
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 property2. Example: static methods
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)); // 12Such 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
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.countis a field shared by all instances, whilethis.instanceCountis unique to each object.
4. Static properties and methods can be typed
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 withstatic.
5. static + private
Static members can be private, meaning accessible only inside the class itself:
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; // private6. Accessing static members inside the class
Inside the class you must access them through the class name, not through this:
class Example {
static count = 0;
increment() {
// this.count++; // error - the instance has no count
Example.count++; // correct
}
}
thisinside 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.
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:
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
thisin 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:
staticmakes 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 readyA concise answer to help you respond confidently on this topic during an interview.