1. Introduction
Most class members belong to individual instances — each object created with 'new' has its own copy. Static members are different: they belong to the class itself, shared across all instances, and are accessed through the class name rather than through an object reference.
Cricket analogy: Each player has their own individual stats like personal strike rate, but a team's total 'matchesPlayed' this season is shared across the whole squad, accessed via the team name rather than any one player, like a static member.
2. Syntax
class ClassName {
static staticProperty: Type = value;
static staticMethod(): ReturnType {
// access other static members via ClassName.member
}
}
ClassName.staticProperty;
ClassName.staticMethod();3. Explanation
A static member is declared with the static keyword and is accessed via the class name (e.g. Counter.getCount()), never via an instance (e.g. NOT counterInstance.getCount()). Static members are useful for data or behavior that is shared across every instance, such as a running count of how many objects were created, configuration constants, or factory/helper methods.
Cricket analogy: A static member is declared like static totalMatches = 0 and accessed as Team.totalMatches, never as squadInstance.totalMatches, useful for shared data like the count of matches a franchise has played across all seasons.
Static members can also combine with access modifiers and readonly, e.g. private static count = 0 restricts direct external access while still sharing the value across all instances, and static readonly maxAllowed = 5 creates a class-wide constant.
Cricket analogy: private static totalRunsScoredLeagueWide = 0 restricts direct outside access while still sharing the tally across every Team instance, and static readonly maxOversPerInnings = 50 creates a league-wide constant.
Static members belong to the class itself, not to instances. Inside a static method, you refer to other static members using the class name (e.g. Counter.count), not 'this', because a static method has no instance context — it can be called without ever creating a single instance of the class.
4. Example
class Counter {
private static count = 0;
static readonly maxAllowed = 5;
constructor() {
Counter.count++;
}
static getCount(): number {
return Counter.count;
}
}
new Counter();
new Counter();
new Counter();
console.log(Counter.getCount());
console.log(Counter.maxAllowed);5. Output
3
56. Key Takeaways
- static members belong to the class itself, shared across all instances.
- Access static members through the class name, e.g. ClassName.member, not through an instance.
- Static methods have no 'this' referring to an instance; they use the class name to reach other static members.
- static combines with access modifiers (private static) and readonly (static readonly) for shared, protected constants.
- A static member's value updates once and is visible to every instance immediately, since there is only one copy.
Practice what you learned
1. How is a static property or method accessed in TypeScript?
2. In the Counter example, what does Counter.getCount() return after three `new Counter()` calls?
3. Why does the static getCount() method reference Counter.count instead of this.count?
4. What does `static readonly maxAllowed = 5;` create?
5. If Counter.count is declared 'private static', can it be accessed as `new Counter().count` from outside the class?
Was this page helpful?
You May Also Like
Classes in TypeScript
Learn how TypeScript classes add type-checked properties, methods, and constructor parameter property shorthand on top of ES2015 classes.
Access Modifiers in TypeScript (public, private, protected)
Control the visibility of class members with public, private, and protected, and understand that these checks exist only at compile time.
Readonly Properties in TypeScript
Use the readonly modifier to prevent a class property from being reassigned after it is initially set in the constructor or at declaration.
Getters and Setters in TypeScript
Use get and set accessors to expose class properties that run custom logic, such as validation, while still being accessed with plain property syntax.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics