Quick revision
The short answer
javac computes reachability with fixed structural rules: a statement is reachable if the one before it can complete normally, and a loop with a constant true condition and no break cannot complete normally. Constant expressions feed into those rules, so with static final boolean DEBUG = false, while (DEBUG) { } has an unreachable body and fails, but if DEBUG is initialised from a method call it is no longer a constant and the same code compiles. Definite assignment uses the same flow analysis, catch clauses are rejected when an earlier clause already catches their type, multi-catch alternatives cannot be subtypes of each other, and constants used in if are inlined and dead code removed, so changing a constant requires recompiling the classes that use it.
What to remember
In this answer, we saw that javac decides reachability with structural rules based on completing normally, and that constant expressions feed into them, so changing a static final field from a literal to a method call can make while (DEBUG) compile. Definite assignment uses the same flow analysis and also treats constant conditions specially. Catch clauses for a subclass after its superclass are unreachable, multi-catch alternatives cannot be related by subclassing, and constants are inlined into using classes, so changing them requires recompiling dependants.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
javac computes reachability with fixed structural rules: a statement is reachable if the one before it can complete normally, and a loop with a constant true condition and no break cannot complete normally. Constant expressions feed into those rules, so with static final boolean DEBUG = false, while (DEBUG) { } has an unreachable body and fails, but if DEBUG is initialised from a method call it is no longer a constant and the same code compiles. Definite assignment uses the same flow analysis, catch clauses are rejected when an earlier clause already catches their type, multi-catch alternatives cannot be subtypes of each other, and constants used in if are inlined and dead code removed, so changing a constant requires recompiling the classes that use it.
Why this question is asked
Most developers meet these rules as error messages: unreachable statement, variable might not have been initialized, exception has already been caught. Underneath, they are one analysis that javac runs over every method, and its results depend on whether an expression is a compile-time constant. That dependency produces strange effects, such as code that compiles or fails depending on how a field is initialised, and stale values in libraries that were not recompiled. Interviewers ask this to see whether we can connect those effects to the rules. In this answer, we will recap the reachability rules, look at constant expressions and the example from the question, see how definite assignment shares the same analysis, look at catch ordering, and finish with constant inlining across classes.
1A Quick Recap of the Reachability Rules
The Java Language Specification defines reachability with rules for each kind of statement, not by simulating the program. The core idea is completing normally: a statement completes normally when control can flow off its end to the next statement. return, throw, break and continue never complete normally. A block completes normally if its last statement does. A while loop completes normally if its condition is not the constant true, or if a reachable break exits it.
From that, a statement in a block is reachable if the statement before it can complete normally, and it is a compile-time error for a statement to be unreachable. A method with a non-void return type must not be able to complete normally either, which is why a method that ends with while (true) without a break needs no return at the end.
2Constant Expressions Change the Answer
The rules treat a condition specially only when it is a constant expression, a value the compiler can compute at compile time. Literals, operators on constants, and constant variables count. A constant variable is a final variable of primitive or String type that is initialised with a constant expression. A final variable initialised from a method call is not a constant, even if the method always returns the same value.
That is the example the question asks for. With DEBUG initialised to the literal false, while (DEBUG) has a constant false condition, so its body is unreachable and the class does not compile. Change only the initialiser to a method call, and DEBUG is no longer a constant. The compiler must assume the condition might be true, the body becomes reachable, and the same loop compiles.
| Declaration | Constant? | while (DEBUG) { step(); } | if (DEBUG) { step(); } |
|---|---|---|---|
| static final boolean DEBUG = false; | yes | error: body unreachable | compiles, block omitted from bytecode |
| static final boolean DEBUG = !true; | yes | error: body unreachable | compiles, block omitted |
static final boolean DEBUG = Boolean.getBoolean("debug"); | no | compiles | compiles, block kept |
| static boolean DEBUG = false; (not final) | no | compiles | compiles, block kept |
Notice that both fields are static final booleans with the value false at run time. The only difference is whether the compiler can see the value, and that alone decides whether the code compiles.
3Definite Assignment Uses the Same Flow Analysis
Java also requires every local variable to be definitely assigned before it is read: on every path that can reach the read, the variable must have been given a value. The compiler checks this with the same flow analysis. Paths that cannot complete normally do not count, which is why a variable assigned only before a break out of an infinite loop is still definitely assigned after the loop.
Constant expressions matter here too. The rules say that after a constant expression whose value is true, a variable counts as assigned when false, because that path can never happen. So with a constant true condition, assigning in the if branch alone is enough, while with a non-constant condition the same code fails.
So reachability and definite assignment are two questions asked of the same picture of the method: which paths exist, and what has been assigned on each of them.
4Catch Clause Ordering
Catch clauses are checked in order, and the first one whose type matches handles the exception. If an earlier clause catches a superclass, a later clause for its subclass can never run, so the compiler rejects it as unreachable, saying the exception has already been caught. Multi-catch, catch (A | B e), has a related rule: the alternatives must not be subclasses of each other, because the narrower one would be redundant.
- Collect the checked exceptions the try block can throw
- Does an earlier clause already catch this type or a supertype?YesError: already caughtNoNext check
- Is it a checked type the try can never throw?YesError: never thrown in the tryNoThe clause is reachable
- Repeat for the next clause
4 steps in order. Play them, or step through them one at a time.
5Constant Inlining and Recompilation
Finally, constants have an effect across classes. When code uses a constant variable from another class, javac copies its value into the using class at compile time instead of reading the field at run time. With if (Config.DEBUG), javac also removes the disabled block entirely. So if a library changes DEBUG from false to true and is rebuilt, a client compiled against the old library still has the old value baked in, and the debug code is not even in its class file. Only recompiling the client picks up the change.
This is why public constants that may change between releases should not be constant variables. Initialising them through a method, for example static final boolean DEBUG = Boolean.parseBoolean("false") or a value read from configuration, keeps them out of inlining, at the cost of losing dead-code removal.
6Follow-up questions
Why does a final local variable count as a constant but a final field set in a constructor does not?
A constant variable must be final and initialised with a constant expression in its declaration. A blank final field assigned in a constructor has no initialiser in its declaration, so the compiler cannot treat it as a constant, even if every constructor assigns the same literal.
Why does a method ending in while (true) not need a return?
Because the loop cannot complete normally, so the end of the method is unreachable, and the rule that a non-void method must not complete normally is satisfied. If we add a break, we must also add a return after the loop.
Is the reachability analysis the same as what the JIT does with dead code?
No. javac's analysis is a fixed set of language rules that decide what compiles, and it only uses constant expressions. The JIT removes far more dead code at run time using profiling and actual values, but that never changes whether a program compiles.
7Conclusion
In this answer, we saw that javac decides reachability with structural rules based on completing normally, and that constant expressions feed into them, so changing a static final field from a literal to a method call can make while (DEBUG) compile. Definite assignment uses the same flow analysis and also treats constant conditions specially. Catch clauses for a subclass after its superclass are unreachable, multi-catch alternatives cannot be related by subclassing, and constants are inlined into using classes, so changing them requires recompiling dependants.
Open with
Reachability in Java is decided by fixed rules, not by running the code. A statement is reachable if the one before it can complete normally, return, throw, break and continue never complete normally, and a while loop with a constant true condition and no reachable break never completes normally either, so anything after it is unreachable.
Then, if they want more
Constant expressions are what make this interesting. If I declare static final boolean DEBUG = false, that is a constant variable, so while (DEBUG) with a body fails with unreachable statement. If I initialise DEBUG from a method call, like Boolean.getBoolean, it is no longer a constant, the compiler must assume the loop might run, and the same code compiles.
Definite assignment uses the same flow analysis: a variable assigned on every path that can complete normally is definitely assigned, and constants count too, so with a final local constant true, assigning only in the if branch is enough.
For catch clauses, a subclass caught after its superclass is unreachable and rejected, and multi-catch alternatives cannot be subtypes of each other. Finally, constants are inlined, and if (DEBUG) blocks are removed by javac, so if a library changes such a constant, clients keep the old value until they are recompiled.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how the compiler's 'can complete normally' analysis (JLS reachability rules)...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/284ed329c7/