skip to content

What is a 'blank final' instance field in Java, and what must every constructor do for it?

level: middleimportance: must knowfreq 62%

answer

  1. Declared with no value = blank final
  2. Every constructor ends with it assigned once
  3. Delegate via this(...) to avoid double assignment
  4. Instance block assigns it? No constructor may
  5. Can't combine declaration init + constructor assignment

basics

~20 s

A blank final is a final variable declared without a value, such as private final int id. As an instance field, it must be set exactly once by the end of every constructor: directly, through this(...), or in an instance initializer.

solid answer

~50 s

A blank final is a final variable declared without an initializer; on an instance field, the value is decided at construction. Every constructor must end with it definitely assigned, exactly once, on each path that completes normally. That does not always mean writing the assignment: a constructor can delegate with `this(...)` to one that assigns it and must then not assign it itself, and if an instance initializer block assigns it, no constructor may. Different constructors can choose different values while the once-only guarantee holds. It cannot also be assigned at the declaration, since a field with an initializer is not blank. Since JDK 25 a constructor may assign it before `super(...)`, so an overridden method called by the superclass constructor sees the real value; JDK 27 is the same, and JDK 23 and 24 allowed it only as a preview. Blank finals are the standard way to build immutable objects from constructor parameters.

go deeper

for a junior

Recognizes a blank final as a final variable with no initializer; on an instance field, it gets its value during construction.

for a middle

States that every constructor must end with a blank final instance field definitely assigned exactly once and can spot the missing-else and double-assignment errors.

for a senior

Uses constructor delegation (this(...)) correctly and reasons about definite assignment across all constructor paths.

for a principal

Designs immutable value types around blank finals and constructor invariants, balancing builders vs. telescoping constructors for assignment correctness.

## Definition A **blank final** is a `final` variable **declared without an initializer**. The term covers local variables and static fields as well as instance fields; an instance field looks like this: ```java private final int id; // blank final: no value yet ``` Contrast with a non-blank final, which is given its value where it is declared: `private final int id = 5;`. The word *blank* means 'left empty for now'. On an instance field, the value is supplied **later**, during object construction. A blank `static final` field is assigned by a static initializer instead, so the constructor rules below do not apply to it. ## The obligation it creates A final instance field must be **definitely assigned exactly once**, and a blank one has *not* been assigned at declaration, so the responsibility shifts to **construction**. Concretely: - **Every constructor** must finish with the blank final definitely assigned, on **every** path that completes normally (every branch of every if/switch, etc.). A path that ends by throwing owes nothing. - It must be assigned **at most once**: every assignment must be reachable only while the field is still definitely unassigned. Assigning it twice is a compile error. That does not mean every constructor contains the assignment. There are three ways to meet the obligation: | Way | What the constructors do | |---|---| | Direct assignment | Each one assigns the field itself | | Delegation | A constructor calls `this(...)` and must not assign the field | | Instance initializer block | The block assigns it, and no constructor may | If a class declares three constructors and only two of them assign the blank final, with no block and no delegation, the compiler rejects the third. ## Why blank finals are useful They let the value be **chosen per-object at runtime** while keeping immutability: ```java class User { private final String name; // blank final User(String name) { this.name = name; // value comes from the argument } } ``` You could not do this with a declaration initializer because the value isn't known until a `User` is created. ## Multiple constructors and constructor chaining To avoid duplicating logic, a constructor can **delegate** to another using `this(...)`: ```java class User { private final String name; User(String name) { this.name = name; } // assigns the blank final User() { this("anonymous"); } // delegates; does NOT assign again } ``` The no-arg constructor must **not** assign `name` itself, before or after the `this(...)` call, because the constructor it calls already does. Either position is a compile error. ## Assigning before super(...) Since JDK 25 (JEP 513) a constructor may run statements before its `super(...)` call, and they may assign a blank final instance field declared in the same class. JDK 25 and JDK 27 behave the same; JDK 23 and 24 allowed the assignment only as a preview, and JDK 22 and earlier do not allow it. An overridden method called by the superclass constructor then sees the real value instead of the default: ```java class Base { Base() { show(); } void show() { } } class Child extends Base { private final int x; Child(int v) { x = v; // before super(...): legal since JDK 25 super(); } @Override void show() { System.out.println(x); } // prints v, not 0 } ``` Code before the call may assign such a field but may not read it. ## Conditional assignment is allowed if every path assigns once ```java class Sign { private final int sign; Sign(int value) { if (value >= 0) { sign = 1; } else { sign = -1; } } } ``` This compiles because both branches assign `sign` exactly once and the compiler can prove it. Remove the `else` and it fails: ```java class Sign { private final int sign; Sign(int value) { if (value >= 0) sign = 1; } // does not compile: variable sign might not have been initialized } ``` A **local** blank final is checked differently, because the end-of-constructor rule applies to instance fields. Its rules are definite assignment at each read and definite unassignment at each assignment. `final int s; if (v >= 0) s = 1;` compiles on its own; add `return s;` and it does not, and add `s = 2;` and it does not either, because `s` might already have been assigned. ## Common pitfall: declaration + constructor ```java class User { private final int id = 0; User(int id) { this.id = id; } // does not compile: id was already assigned at declaration } ``` This is two assignments. To set `id` from the constructor, leave it blank at declaration. ## Summary A blank final instance field defers its value to construction. Every constructor must end with it assigned exactly once on every path that completes normally, whether directly, by delegating with `this(...)`, or through an instance initializer block. This is the idiomatic mechanism for immutable objects parameterized by constructor arguments.

  • If two constructors both need the same blank final value, how do you avoid assigning it twice?
    Use constructor delegation: one constructor assigns the field, and the others call it via this(...). The delegating constructor must not assign the field itself.
  • Can a blank final be assigned inside an if-branch without an else?
    For an instance field, only if the other path also gets it assigned or ends by throwing; otherwise the constructor does not compile. A local blank final is looser: a read needs it definitely assigned and a further assignment needs it definitely unassigned, so the lone if compiles when neither follows.
  • Can a blank final be assigned before the constructor's super(...) call?
    Yes, since JDK 25 (preview in 23 and 24), if it is an instance field declared in the same class. It can be assigned there but not read. It cannot be assigned before this(...), because the invoked constructor is responsible for it.

  • Assigning a blank final at declaration AND in a constructor (double assignment)
  • Forgetting one constructor in a multi-constructor class
  • A constructor's if without an else where only the if-branch assigns a blank final instance field
  • Believing every constructor must contain the assignment, even one that delegates with this(...)

assigning a field before super(...)
Java 25pre-release 23

Assumes JDK 27; JDK 25 LTS behaves the same. Assigning a blank final instance field before super(...) needs JDK 25 or later, where flexible constructor bodies became final; JDK 23 and 24 allowed it only as a preview, and JDK 22 and earlier do not allow it.

context