Skip to content

Instantly share code, notes, and snippets.

@JosePaumard
Created November 18, 2025 22:58
Show Gist options
  • Select an option

  • Save JosePaumard/648ca626258aae79e299f315ed03b717 to your computer and use it in GitHub Desktop.

Select an option

Save JosePaumard/648ca626258aae79e299f315ed03b717 to your computer and use it in GitHub Desktop.
How to observe the mutation of a final field before its initialization.
class A {
public A() {
IO.println(message());
}
public String message() {
return "Hello World!";
}
}
class B extends A {
private final String message;
public B(String message) {
this.message = message;
}
public String message() {
return this.message;
}
}
void main() {
var b = new B("Bonjour le monde !");
}
@JosePaumard
Copy link
Author

This problem is fixed by adding an explicit call to super() on line 14, after the initialization of message (Java 25 and later).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment