---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 24 Edition"
java_version: 24
chapter: "Chapter 8. Classes"
section: "8.3.2"
heading: "8.3.2. Field Initialization"
parent: "8.3. Field Declarations"
source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-8.html#jls-8.3.2
id: "8.3.2"
url: https://jvm.obfuscated.software/jls/se24/8.3.2.html
raw: https://jvm.obfuscated.software/jls/se24/8.3.2.md
---

# 8.3.2. Field Initialization

If a declarator in a field declaration has a *variable initializer*, then the declarator has the semantics of an assignment ([§15.26](15.26.md)) to the declared variable.

If the declarator is for a class variable (that is, a `static` field) ([§8.3.1.1](8.3.1.1.md)), then the following rules apply to its initializer:

- The initializer may not refer to the current object using the keyword `this` or the keyword `super`, as specified in [§15.8.3](15.8.3.md) and [§15.11.2](15.11.2.md), nor refer by simple name to any instance variable or instance method, as specified in [§6.5.6.1](6.5.6.1.md) and [§15.12.3](15.12.3.md).

- At run time, the initializer is evaluated and the assignment performed exactly once, when the class is initialized ([§12.4.2](12.4.2.md)).

  Note that `static` fields that are constant variables ([§4.12.4](4.12.4.md)) are initialized before other `static` fields ([§12.4.2](12.4.2.md), step 6). This also applies in interfaces ([§9.3.1](9.3.1.md)). When such fields are referenced by simple name, they will never be observed to have their default initial values ([§4.12.5](4.12.5.md)).

If the declarator is for an instance variable (that is, a field that is not `static`), then the following rules apply to its initializer:

- The initializer may refer to the current object using the keyword `this` or the keyword `super`, and may refer by simple name to any class variable declared in or inherited by the class, even one whose declaration occurs to the right of the initializer ([§3.5](3.5.md)).
- At run time, the initializer is evaluated and the assignment performed each time an instance of the class is created ([§12.5](12.5.md)).

References from variable initializers to fields that may not yet be initialized are restricted, as specified in [§8.3.3](8.3.3.md) and [§16 (*Definite Assignment*)](16.md).

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

Variable initializers are also used in local variable declaration statements ([§14.4](14.4.md)), where the initializer is evaluated and the assignment performed each time the local variable declaration statement is executed.

**Example 8.3.2-1. Field Initialization**

```
class Point {
    int x = 1, y = 5;
}
class Test {
    public static void main(String[] args) {
        Point p = new Point();
        System.out.println(p.x + ", " + p.y);
    }
}
```

This program produces the output:

```
1, 5
```

because the assignments to `x` and `y` occur whenever a new `Point` is created.

**Example 8.3.2-2. Forward Reference to a Class Variable**

```
class Test {
    float f = j;
    static int j = 1;
}
```

This program compiles without error; it initializes `j` to `1` when class `Test` is initialized, and initializes `f` to the current value of `j` every time an instance of class `Test` is created.
