Without static initialization blocks, complex static initialization might be achieved by calling a static method after the class declaration:
class MyClass {
static init() {
}
}
MyClass.init();
However, this approach exposes an implementation detail (the init()
method) to the user of the class. On the other hand, any initialization logic declared outside the class does not have access to private static fields. Static initialization blocks allow arbitrary initialization logic to be declared within the class and executed during class evaluation.
A class
can have any number of static {}
initialization blocks in its class body. These are evaluated, along with any interleaved static field initializers, in the order they are declared. Any static initialization of a super class is performed first, before that of its sub classes.
The scope of the variables declared inside the static block is local to the block. This includes var
, function
, const
, and let
declarations. var
declarations in the block are not hoisted.
var y = "Outer y";
class A {
static field = "Inner y";
static {
var y = this.field;
}
}
console.log(y);
The this
inside a static block refers to the constructor object of the class. super.property
can be used to access static properties of the super class. Note however that it is a syntax error to call super()
in a class static initialization block, or to use the arguments
object.
The statements are evaluated synchronously. You cannot use await
or yield
in this block. (Think of the initialization statements as being implicitly wrapped in a function.)
The scope of the static block is nested within the lexical scope of the class body, and can access private names declared within the class without causing a syntax error.
Static field initializers and static initialization blocks are evaluated one-by-one. The initialization block can refer to field values above it, but not below it. All static methods are added beforehand and can be accessed, although calling them may not behave as expected if they refer to fields below the current block.
Note: This is more important with private static fields, because accessing a non-initialized private field throws a TypeError
, even if the private field is declared below. (If the private field is not declared, it would be an early SyntaxError
.)
A static initialization block may not have decorators (the class itself may).