---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 8. Classes"
section: "8.7"
heading: "8.7. Static Initializers"
parent: "Chapter 8. Classes"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.7
id: "8.7"
url: https://jvm.obfuscated.software/jls/se8/8.7.html
raw: https://jvm.obfuscated.software/jls/se8/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.21](14.21.md)).

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

It is a compile-time error if the keyword `this` ([§15.8.3](15.8.3.md)) or the keyword `super` ([§15.11](15.11.md), [§15.12](15.12.md)) or any type variable declared outside the static initializer, appears anywhere within a static initializer.

Use of class variables whose declarations appear textually after the use is sometimes restricted, even though these class variables are in scope. See [§8.3.3](8.3.3.md) for the precise rules governing forward reference to class variables.

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