DEV Community

Cover image for JLS Deep Dive: Variables & Definite Assignment — When is a Value 'Safe'?
Michele Gallotti
Michele Gallotti

Posted on

JLS Deep Dive: Variables & Definite Assignment — When is a Value 'Safe'?

Before you can write safe code, you must understand how the compiler proves it's safe. In Java, this proof is called Definite Assignment.

Defined in JLS Chapter 16, it's a static analysis that guarantees no local variable is ever read before a value has been assigned to it. This mechanism is the reason local variables don't have default values, and it's what allows the compiler to be so strict about initialization paths.

This sixth chapter in our Java SE 25 deep dive explores the world of variables from the compiler's perspective.

In this article, you'll learn:

  • The 8 kinds of variables recognized by the JLS, from instance variables to lambda parameters, and their different initialization strategies.
  • The formal distinction between a final variable and an effectively final one, and why this matters for lambdas and anonymous classes.
  • How the compiler's flow analysis navigates short-circuit operators (&& and ||) to determine if a variable is assigned in all possible code paths.
  • The concept of Definite Unassignment, the mirror image of definite assignment, which ensures a final variable is assigned exactly once.

This is a guide to the rules that make Java a memory-safe language, grounded in the official JLS documentation.

Read the full article: https://eldritch.codes/en/tome/tome-02-jvm-runtime/chapter-04


Part of the "Eldritch Codes" series — a comprehensive guide to Java SE 25, explored through the lens of the JLS and the original specs.

Top comments (0)