What are static methods and properties, and when do you use them?
Short answer
static members belong to the class itself, not to instances — you call them as ClassName.method() rather than instance.method(). They're useful for factory methods, utility functions, or shared counters tied to the class as a whole.
Advertisement
AModel answer
static members belong to the class itself, not to instances — you call them as ClassName.method() rather than instance.method(). They're useful for factory methods, utility functions, or shared counters tied to the class as a whole.
class User {
static count = 0;
constructor() { User.count++; }
static fromJSON(json) { return new User(JSON.parse(json)); }
}Real-world example
A static User.fromJSON() factory method that builds a User instance from an API response.
Likely follow-ups
- Can static methods access instance properties directly?
Related topics