Static members and static blocks — properties of the class itself¶
Level: 201 · for anyone who has written static create() and called it on a subclass
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: A static member is a property of the class function, not of its instances; class B extends A makes A the prototype of B, so B inherits A's statics — and this.count++ inside an inherited static method creates a separate count on B.
Keywords: static, static {}
What the finished page will answer¶
- Where does a static method live, and why is
new Base().createundefined? - What is
thisinside a static method called asChild.create(), and what doesnew this()build there? - After
Child.create()runsthis.count++, what areBase.countandChild.count, and how do you keep one shared counter? - When does a
static { }block run, in what order relative to static fields, and what private names can it read? - What does
tscreport when a static method is called on an instance?
Examples it will need¶
-
static_members_inherited_js.js— a static counter and factory called on a base class and on a subclass, the prototype link between the two classes, and the two separate counts -
static_members_block_order_js.js— static fields and two static blocks logging as the class is defined, in source order, before any instance exists -
static_members_on_instance_tserror.ts— tsc's TS2576 error for calling a static method on an instance: did you mean to access the static member?
See also¶
extendsandsuper— extends links the two classes as well as their instances- The prototype chain — why Child.count reads Base.count, then shadows it
- Classes — the function object statics are properties of
- Private fields — static #fields, private to the class
- Functions are objects — a function holding properties, before classes existed
- Rust:
implblocks ↗ — how Rust spells a static method: an associated function called on the type