---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 27 Edition"
java_version: 27
chapter: "Chapter 8. Classes"
section: "8.7"
heading: "8.7. Static Initializers"
parent: "Chapter 8. Classes"
source: https://docs.oracle.com/javase/specs/jls/se27/html/jls-8.html#jls-8.7
id: "8.7"
url: https://jvm.obfuscated.software/jls/se27/8.7.html
raw: https://jvm.obfuscated.software/jls/se27/8.7.md
---

# 8.7. Static Initializers

A *static initializer* declared in a class is executed when the class is initialized ([§12.4.2](12.4.2.md)). Together with any field initializers for class variables ([§8.3.2](8.3.2.md)), static initializers may be used to initialize the class variables of the class.

```
StaticInitializer:
  static Block
```

It is a compile-time error if a static initializer cannot complete normally ([§14.22](14.22.md)).

It is a compile-time error if a `return` statement ([§14.17](14.17.md)) appears anywhere within a static initializer.

A static initializer introduces a static context ([§8.1.3](8.1.3.md)), which limits the use of constructs that refer to the current object. Notably, the keywords `this` and `super` are prohibited in a static context ([§15.8.3](15.8.3.md), [§15.11.2](15.11.2.md)), as are unqualified references to instance variables, instance methods, and type parameters of lexically enclosing declarations ([§6.5.5.1](6.5.5.1.md), [§6.5.6.1](6.5.6.1.md), [§15.12.3](15.12.3.md)).

Restrictions on how a static initializer may refer to class variables, even when the class variables are in scope, are specified in [§8.3.3](8.3.3.md).

Exception checking for a static initializer is specified in [§11.2.3](11.2.3.md).
