Skip to content

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().create undefined?
  • What is this inside a static method called as Child.create(), and what does new this() build there?
  • After Child.create() runs this.count++, what are Base.count and Child.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 tsc report 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

Sources to start from