Blank Final and Static Final Variables

Naveen AnamaneniUpdated · 4 min read

A PNR number is fixed for each ticket, but it isn't known until the ticket is booked: every ticket has a different one. So it can't be a constant written into the class, yet it must never change once set. Java handles this with a blank final: a final field declared without a value, which each constructor must assign exactly once.

Blank Final Instance Fields

Tickets.java▶ Try it
public class Tickets {
    static class Ticket {
        final String pnr;
        final String passenger;
        int berth;
 
        Ticket(String pnr, String passenger) {
            this.pnr = pnr;
            this.passenger = passenger;
        }
    }
 
    public static void main(String[] args) {
        Ticket a = new Ticket("4512345678", "Navika Anamaneni");
        Ticket b = new Ticket("4598765432", "Advitha Anamaneni");
        a.berth = 23;
        a.berth = 31;
        System.out.println(a.pnr + " " + a.passenger + ", berth " + a.berth);
        System.out.println(b.pnr + " " + b.passenger);
    }
}
Output
4512345678 Navika Anamaneni, berth 31
4598765432 Advitha Anamaneni

How it works

  1. Lines 3 and 4: pnr and passenger are blank finals: final, but no value where they're declared.
  2. The constructor assigns each one exactly once. Each ticket gets its own value, fixed for life.
  3. berth isn't final, so it can change (lines 16 and 17).

Try it yourself: add a.pnr = "1111111111"; in main. Read the error.

What the Compiler Checks

If any constructor can finish without assigning it:

Compiler output
Blank.java:1: error: variable pnr might not have been initialized
class Ticket { final String pnr; Ticket() {} }
                                           ^

With several constructors, chaining them to one constructor that assigns all blank finals (see "Constructor Overloading and Constructor Chaining") makes it easy to get right.

Static Final: Class Constants

KindDeclaredAssignedOne value per
Final instance fieldfinal String pnr;In each constructor (or an instance block)Object
Static final (constant)static final int MAX_SEATS = 72;At declarationClass
Blank static finalstatic final String BUILD;In a static blockClass

A blank static final is useful when the value needs a calculation or a check:

Settings.java▶ Try it
public class Settings {
    static class AppInfo {
        static final String BUILD;
 
        static {
            int year = 2026, month = 11;
            BUILD = "PoolString-" + year + "." + month;
        }
    }
 
    public static void main(String[] args) {
        System.out.println(AppInfo.BUILD);
    }
}
Output
PoolString-2026.11

Compile-Time Constants

A static final of a primitive or String type, assigned a constant expression at declaration (static final int MAX = 72;), is a compile-time constant. The compiler copies its value straight into the code that uses it. Two consequences:

  • It can be used in case labels of a switch.
  • If you change it, classes that use it must be recompiled to see the new value.

Common Mistakes

Forgetting to assign a blank final in one constructor. The compiler reports might not have been initialized.

Assigning it twice. For example in an instance block and again in the constructor: variable pnr might already have been assigned.

Making a per-object value static final. A static final has one value for the whole class; a PNR must be a final instance field.

Interview Corner

What is a blank final variable?

A final variable declared without an initializer. A blank final instance field must be assigned exactly once in every constructor or in an instance initializer; a blank static final must be assigned exactly once in a static block. After that, it can't change.

Practice

  1. Easy: Add a blank final aadhaarLast4 to a Customer class and set it in the constructor.
  2. Harder: Create a class with two constructors and a blank final id. Make both constructors set it by chaining to one of them.

Quick check: Blank final

4 questions
1Where must a blank final instance field be assigned?
2Where is a blank static final assigned?
3What error appears when a constructor forgets a blank final?
4Which should a PNR number be in a Ticket class?

Key Takeaways

Was this tutorial helpful?

Lesson 111 of 482