Glossary / Static class members
Static class members
Static fields and methods belong to a class constructor rather than each instance. They are useful for factories, type-level operations, constants, and shared state that genuinely belongs to the class. Static members participate in constructor inheritance: a subclass constructor can inherit static methods from its parent.
Receiver and initialization
class User {
static kind = "user";
static create(record) { return new this(record.name); }
constructor(name) { this.name = name; }
}
class Admin extends User {}
Admin.create({ name: "Ada" }) instanceof Admin; // true
Here this inside the static method is the constructor used as the receiver (Admin), so new this(...) produces an Admin. A detached static method may lose that receiver just like a detached instance method. Static initialization blocks can perform one-time initialization with access to private static elements. Static fields are initialized in class evaluation order, which matters for inheritance and circular module initialization.
Mutable static state is shared across all instances and can create test-order coupling or global state hidden behind a class name. A static method that does not conceptually belong to a type may be clearer as a module function. Static fields are not instance defaults; define instance fields separately.
Interview questions
- Where are static methods stored? On the class constructor, not its instance prototype.
- What is
thisin a static method? The constructor receiver on which the method was invoked. - Do subclasses inherit static methods? Usually yes, through constructor prototype inheritance.
- What risk does mutable static state create? Shared behavior and test-order/environment coupling.