GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Resources
  • HTML
  • CSS
  • JavaScript
  • Performance
  • Accessibility
  • Identity
  • Learn HTML
  • Learn CSS
  • Learn JavaScript
  • Learn AI
  • Learn Performance
  • Learn Accessibility
  • AI and the web

This view

  • Original page
  • New address

GeezerVison is showing web.dev — Static initialization blocks | web.dev

Modern Web Guidance is now stable! Upgrade your coding agents with expert-vetted web development best practices. Read the announcement.

Static initialization blocks Stay organized with collections Save and categorize content based on your preferences.

Static initialization blocks are a very new JavaScript feature, standardized in ECMAScript 2022 and only supported in modern browsers. Static initialization blocks let you set the value of static fields dynamically, using logic that spans multiple statements.

To create a static initialization block, use the static keyword followed by a block statement in curly brackets ({}):

class MyClass {
  static {}
}

This pattern lets you initialize or alter static fields declared in the body of the class:

class MyClass {
  static firstProperty = 'First property.';
  static secondProperty;
  static {
    this.secondProperty = 'Second property.';
  }
}

MyClass.secondProperty;
"Second property."

These statements are evaluated at the time a class is first initialized, that is, when the JavaScript engine first evaluates it, not when an instance of a class is created, as with constructor():

class MyClass {
    static {
        console.log( "Static initialization block." );
    }
    constructor() {
        console.log( "Constructor." );
    }
}
> "Static initialization block."

const myClassInstance = new MyClass();
> "Constructor."

A class can contain multiple static initialization blocks, and those blocks are evaluated in the order they're declared, alongside any other static fields and methods. This means that only the fields declared before a static initialization block are available in that block:

class MyClass {
  static myNewField;
  static {
    this.myNewField = this.myField;
  }
  static myField = "My value.";
};

MyClass.myNewField;
> undefined

class MyCorrectedClass {
  static myNewField;
  static myField = "My value.";
  static {
    this.myNewField = this.myField;
  }
};

MyCorrectedClass.myNewField;
> "My value."

Check your understanding

Which of the following statements are true?

Static initialization blocks are evaluated when the class is first initialized.
A class can only contain one static initialization block.
Fields declared after a static initialization block are available inside the block.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-03-31 UTC.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.