---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 26 Edition"
java_version: 26
chapter: "Chapter 16. Definite Assignment"
section: "16.2"
heading: "16.2. Definite Assignment and Statements"
parent: "Chapter 16. Definite Assignment"
source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-16.html#jls-16.2
id: "16.2"
url: https://jvm.obfuscated.software/jls/se26/16.2.html
raw: https://jvm.obfuscated.software/jls/se26/16.2.md
---

# 16.2. Definite Assignment and Statements

## 16.2.1. Empty Statements

- V is [un]assigned after an empty statement ([§14.6](14.6.md)) iff it is [un]assigned before the empty statement.

## 16.2.2. Blocks

- A blank `final` member field V is definitely assigned (and moreover is not definitely unassigned) before the block ([§14.2](14.2.md)) that is the body of any method in the scope of V and before the declaration of any class declared within the scope of V.

- A local variable V declared by a statement S is definitely unassigned (and moreover is not definitely assigned) before the block that is the body of the method, instance initializer or static initializer which contains S.

- Let C be a class declared within the scope of V. Then V is definitely assigned before the block that is the body of any method, instance initializer, or static initializer declared in C iff V is definitely assigned before the declaration of C.

  Note that there are no rules that would allow us to conclude that V is definitely unassigned before the block that is the body of any method, instance initializer, or static initializer declared in C. We can informally conclude that V is not definitely unassigned before the block that is the body of any method, instance initializer, or static initializer declared in C, but there is no need for such a rule to be stated explicitly.

- V is [un]assigned after an empty block iff V is [un]assigned before the empty block.

- V is [un]assigned after a non-empty block iff V is [un]assigned after the last statement in the block.

- V is [un]assigned before the first statement of the block iff V is [un]assigned before the block.

- V is [un]assigned before any other statement S of the block iff V is [un]assigned after the statement immediately preceding S in the block.

We say that V is definitely unassigned everywhere in a block B iff:

- V is definitely unassigned before B.
- V is definitely assigned after `e` in every assignment expression V `=` `e`, V `+=` `e`, V `-=` `e`, V `*=` `e`, V `/=` `e`, V `%=` `e`, V `<<=` `e`, V `>>=` `e`, V `>>>=` `e`, V `&=` `e`, V `|=` `e`, or V `^=` `e` that occurs in B.
- V is definitely assigned before every expression `++`V, `--`V, V`++`, or V`--` that occurs in B.

These conditions are counterintuitive and require some explanation. Consider a simple assignment V `=` `e`. If V is definitely assigned after `e`, then either:

- The assignment occurs in dead code, and V is vacuously definitely assigned. In this case, the assignment will not actually take place, and we can assume that V is not being assigned by the assignment expression. Or:
- V was already assigned by an earlier expression prior to `e`. In this case the current assignment will cause a compile-time error.

So, we can conclude that if the conditions are met by a program that causes no compile time error, then any assignments to V in B will not actually take place at run time.

## 16.2.3. Local Class and Interface Declarations

- V is [un]assigned after a local class or interface declaration ([§14.3](14.3.md)) iff V is [un]assigned before the local class or interface declaration.

## 16.2.4. Local Variable Declaration Statements

- V is [un]assigned after a local variable declaration statement ([§14.4.2](14.4.2.md)) that contains no variable initializers iff V is [un]assigned before the local variable declaration statement.
- V is definitely assigned after a local variable declaration statement that contains at least one variable initializer iff either V is definitely assigned after the last variable initializer in the local variable declaration statement or the last variable initializer in the declaration is in the declarator that declares V.
- V is definitely unassigned after a local variable declaration statement that contains at least one variable initializer iff V is definitely unassigned after the last variable initializer in the local variable declaration statement and the last variable initializer in the declaration is not in the declarator that declares V.
- V is [un]assigned before the first variable initializer in a local variable declaration statement iff V is [un]assigned before the local variable declaration statement.
- V is definitely assigned before any variable initializer `e` other than the first one in the local variable declaration statement iff either V is definitely assigned after the variable initializer to the left of `e` or the initializer expression to the left of `e` is in the declarator that declares V.
- V is definitely unassigned before any variable initializer `e` other than the first one in the local variable declaration statement iff V is definitely unassigned after the variable initializer to the left of `e` and the initializer expression to the left of `e` is not in the declarator that declares V.

## 16.2.5. Labeled Statements

- V is [un]assigned after a labeled statement `L` `:` S (where `L` is a label) ([§14.7](14.7.md)) iff V is [un]assigned after S and V is [un]assigned before every `break` statement that may exit the labeled statement `L` `:` S.
- V is [un]assigned before S iff V is [un]assigned before `L` `:` S.

## 16.2.6. Expression Statements

- V is [un]assigned after an expression statement `e;` ([§14.8](14.8.md)) iff it is [un]assigned after `e`.
- V is [un]assigned before `e` iff it is [un]assigned before `e;`.

## 16.2.7. `if` Statements

The following rules apply to a statement `if (e) S` ([§14.9.1](14.9.1.md)):

- V is [un]assigned after `if (e) S` iff V is [un]assigned after S and V is [un]assigned after `e` when false.
- V is [un]assigned before `e` iff V is [un]assigned before `if (e) S`.
- V is [un]assigned before S iff V is [un]assigned after `e` when true.

The following rules apply to a statement `if (e) S else T` ([§14.9.2](14.9.2.md)):

- V is [un]assigned after `if (e) S else T` iff V is [un]assigned after S and V is [un]assigned after T.
- V is [un]assigned before `e` iff V is [un]assigned before `if (e) S else T`.
- V is [un]assigned before S iff V is [un]assigned after `e` when true.
- V is [un]assigned before T iff V is [un]assigned after `e` when false.

## 16.2.8. `assert` Statements

The following rules apply both to a statement `assert e_1` and to a statement `assert e_1 : e_2` ([§14.10](14.10.md)):

- V is [un]assigned before `e_1` iff V is [un]assigned before the `assert` statement.
- V is definitely assigned after the `assert` statement iff V is definitely assigned before the `assert` statement.
- V is definitely unassigned after the `assert` statement iff V is definitely unassigned before the `assert` statement and V is definitely unassigned after `e_1` when true.

The following rule applies to a statement `assert e_1 : e_2`:

- V is [un]assigned before `e_2` iff V is [un]assigned after `e_1` when false.

## 16.2.9. `switch` Statements

- V is [un]assigned after a `switch` statement ([§14.11](14.11.md)) iff all of the following are true:

  - V is [un]assigned before every `break` statement ([§14.15](14.15.md)) that may exit the `switch` statement.
  - For each switch rule ([§14.11.1](14.11.1.md)) in the switch block, V is [un]assigned after the switch rule expression, switch rule block, or switch rule `throw` statement introduced by the switch rule.
  - If there is a switch labeled statement group in the switch block, then V is [un]assigned after the last block statement of the last switch labeled statement group.
  - If the `switch` statement is not exhaustive ([§14.11.1.1](14.11.1.1.md)), or if the switch block ends with a switch label followed by the `}` separator, then V is [un]assigned after the selector expression.

- V is [un]assigned before the selector expression of a `switch` statement iff V is [un]assigned before the `switch` statement.

- V is [un]assigned before any guard associated with the switch block of a `switch` statement iff V is [un]assigned after the selector expression.

- V is [un]assigned before the switch rule expression, switch rule block, or switch rule `throw` statement introduced by a switch rule in the switch block iff V is [un]assigned after the selector expression of the `switch` statement.

- V is [un]assigned before the first block statement of a switch labeled statement group in the switch block iff both of the following are true:

  - V is [un]assigned after the selector expression of the `switch` statement.
  - If the switch labeled statement group is not the first in the switch block, V is [un]assigned after the last block statement of the preceding switch labeled statement group.

- V is [un]assigned before a block statement that is not the first of a switch labeled statement group in the switch block iff V is [un]assigned after the preceding block statement.

## 16.2.10. `while` Statements

- V is [un]assigned after `while (e) S` ([§14.12](14.12.md)) iff V is [un]assigned after `e` when false and V is [un]assigned before every `break` statement for which the `while` statement is the break target.

- V is definitely assigned before `e` iff V is definitely assigned before the `while` statement.

- V is definitely unassigned before `e` iff all of the following are true:

  - V is definitely unassigned before the `while` statement.
  - Assuming V is definitely unassigned before `e`, V is definitely unassigned after S.
  - Assuming V is definitely unassigned before `e`, V is definitely unassigned before every `continue` statement for which the `while` statement is the continue target.

- V is [un]assigned before S iff V is [un]assigned after `e` when true.

## 16.2.11. `do` Statements

- V is [un]assigned after `do S while (e);` ([§14.13](14.13.md)) iff V is [un]assigned after `e` when false and V is [un]assigned before every `break` statement for which the `do` statement is the break target.

- V is definitely assigned before S iff V is definitely assigned before the `do` statement.

- V is definitely unassigned before S iff all of the following are true:

  - V is definitely unassigned before the `do` statement.
  - Assuming V is definitely unassigned before S, V is definitely unassigned after `e` when true.

- V is [un]assigned before `e` iff V is [un]assigned after S and V is [un]assigned before every `continue` statement for which the `do` statement is the continue target.

## 16.2.12. `for` Statements

The rules herein cover the basic `for` statement ([§14.14.1](14.14.1.md)). Since the enhanced `for` statement ([§14.14.2](14.14.2.md)) is defined by translation to a basic `for` statement, no special rules need to be provided for it.

- V is [un]assigned after a `for` statement iff both of the following are true:

  - Either a condition expression is not present or V is [un]assigned after the condition expression when false.
  - V is [un]assigned before every `break` statement for which the `for` statement is the break target.

- V is [un]assigned before the initialization part of the `for` statement iff V is [un]assigned before the `for` statement.

- V is definitely assigned before the condition part of the `for` statement iff V is definitely assigned after the initialization part of the `for` statement.

- V is definitely unassigned before the condition part of the `for` statement iff both of the following are true:

  - V is definitely unassigned after the initialization part of the `for` statement.
  - Assuming V is definitely unassigned before the condition part of the `for` statement, V is definitely unassigned after the incrementation part of the `for` statement.

- V is [un]assigned before the contained statement iff either of the following is true:

  - A condition expression is present and V is [un]assigned after the condition expression when true.
  - No condition expression is present and V is [un]assigned before the condition part of the `for` statement.

- V is [un]assigned before the incrementation part of the `for` statement iff V is [un]assigned after the contained statement and V is [un]assigned before every `continue` statement for which the `for` statement is the continue target.

### 16.2.12.1. Initialization Part of `for` Statement

- If the initialization part of the `for` statement is a local variable declaration statement, the rules of [§16.2.4](16.2.4.md) apply.

- Otherwise, if the initialization part is empty, then V is [un]assigned after the initialization part iff V is [un]assigned before the initialization part.

- Otherwise, three rules apply:

  - V is [un]assigned after the initialization part iff V is [un]assigned after the last expression statement in the initialization part.
  - V is [un]assigned before the first expression statement in the initialization part iff V is [un]assigned before the initialization part.
  - V is [un]assigned before an expression statement S other than the first in the initialization part iff V is [un]assigned after the expression statement immediately preceding S.

### 16.2.12.2. Incrementation Part of `for` Statement

- If the incrementation part of the `for` statement is empty, then V is [un]assigned after the incrementation part iff V is [un]assigned before the incrementation part.

- Otherwise, three rules apply:

  - V is [un]assigned after the incrementation part iff V is [un]assigned after the last expression statement in the incrementation part.
  - V is [un]assigned before the first expression statement in the incrementation part iff V is [un]assigned before the incrementation part.
  - V is [un]assigned before an expression statement S other than the first in the incrementation part iff V is [un]assigned after the expression statement immediately preceding S.

## 16.2.13. `break`, `yield`, `continue`, `return`, and `throw` Statements

- By convention, we say that V is [un]assigned after any `break`, `yield`, `continue`, `return`, or `throw` statement ([§14.15](14.15.md), [§14.21](14.21.md), [§14.16](14.16.md), [§14.17](14.17.md), [§14.18](14.18.md)).

  The notion that a variable is "[un]assigned after" a statement or expression really means "is [un]assigned after the statement or expression completes normally". Because a `break`, `yield`, `continue`, `return`, or `throw` statement never completes normally, it vacuously satisfies this notion.

- In a `yield` statement with expression `e`, or a `return` statement with expression `e`, or a `throw` statement with expression `e`, V is [un]assigned before `e` iff V is [un]assigned before the `yield`, `return`, or `throw` statement.

## 16.2.14. `synchronized` Statements

- V is [un]assigned after `synchronized (e) S` ([§14.19](14.19.md)) iff V is [un]assigned after S.
- V is [un]assigned before `e` iff V is [un]assigned before the statement `synchronized (e) S`.
- V is [un]assigned before S iff V is [un]assigned after `e`.

## 16.2.15. `try` Statements

The rules herein cover the `try`-`catch` and `try`-`catch`-`finally` statements ([§14.20.1](14.20.1.md), [§14.20.2](14.20.2.md)). Since the `try`-with-resources statement ([§14.20.3](14.20.3.md)) is defined by translation to a `try`-`catch`-`finally` statement, no special rules need to be provided for it.

These rules apply to every `try` statement ([§14.20](14.20.md)), whether or not it has a `finally` block:

- V is [un]assigned before the `try` block iff V is [un]assigned before the `try` statement.

- V is definitely assigned before a `catch` block iff V is definitely assigned before the `try` block.

- V is definitely unassigned before a `catch` block iff all of the following are true:

  - V is definitely unassigned after the `try` block.
  - V is definitely unassigned before every `return` statement that belongs to the `try` block.
  - V is definitely unassigned after `e` in every statement of the form `throw` `e` that belongs to the `try` block.
  - V is definitely unassigned after every `assert` statement that occurs in the `try` block.
  - V is definitely unassigned before every `break` statement that belongs to the `try` block and whose break target contains (or is) the `try` statement.
  - V is definitely unassigned before every `continue` statement that belongs to the `try` block and whose continue target contains the `try` statement.

If a `try` statement does not have a `finally` block, then this rule also applies:

- V is [un]assigned after the `try` statement iff V is [un]assigned after the `try` block and V is [un]assigned after every `catch` block in the `try` statement.

If a `try` statement does have a `finally` block, then these rules also apply:

- V is definitely assigned after the `try` statement iff at least one of the following is true:

  - V is definitely assigned after the `try` block and V is definitely assigned after every `catch` block in the `try` statement.
  - V is definitely assigned after the `finally` block.

- V is definitely unassigned after the `try` statement iff V is definitely unassigned after the `finally` block.

- V is definitely assigned before the `finally` block iff V is definitely assigned before the `try` statement.

- V is definitely unassigned before the `finally` block iff all of the following are true:

  - V is definitely unassigned after the `try` block.
  - V is definitely unassigned before every `return` statement that belongs to the `try` block.
  - V is definitely unassigned after `e` in every statement of the form `throw` `e` that belongs to the `try` block.
  - V is definitely unassigned after every `assert` statement that occurs in the `try` block.
  - V is definitely unassigned before every `break` statement that belongs to the `try` block and whose break target contains (or is) the `try` statement.
  - V is definitely unassigned before every `continue` statement that belongs to the `try` block and whose continue target contains the `try` statement.
  - V is definitely unassigned after every `catch` block of the `try` statement.
