It is a compile-time error if a statement cannot be executed because it is unreachable.
This section is devoted to a precise explanation of the word "reachable." The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer, or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.
For example, a Java compiler will accept the code:
{
int n = 5;
while (n > 7) k = 2;
}
even though the value of n is known at compile time and in principle it can be known at compile time that the assignment to k can never be executed.
The rules in this section define two technical terms:
- whether a statement is reachable
- whether a statement can complete normally
The definitions here allow a statement to complete normally only if it is reachable.
To shorten the description of the rules, the customary abbreviation "iff" is used to mean "if and only if."
A reachable break statement exits a statement if, within the break target, either there are no try statements whose try blocks contain the break statement, or there are try statements whose try blocks contain the break statement and all finally clauses of those try statements can complete normally.
This definition is based on the logic around "attempts to transfer control" in §14.15.
A continue statement continues a do statement if, within the do statement, either there are no try statements whose try blocks contain the continue statement, or there are try statements whose try blocks contain the continue statement and all finally clauses of those try statements can complete normally.
The rules are as follows:
-
The block that is the body of a constructor, method, instance initializer, or static initializer is reachable.
-
An empty block that is not a switch block can complete normally iff it is reachable.
A non-empty block that is not a switch block can complete normally iff the last statement in it can complete normally.
The first statement in a non-empty block that is not a switch block is reachable iff the block is reachable.
Every other statement
Sin a non-empty block that is not a switch block is reachable iff the statement precedingScan complete normally. -
A local class declaration statement can complete normally iff it is reachable.
-
A local variable declaration statement can complete normally iff it is reachable.
-
An empty statement can complete normally iff it is reachable.
-
A labeled statement can complete normally if at least one of the following is true:
- The contained statement can complete normally.
- There is a reachable
breakstatement that exits the labeled statement.
The contained statement is reachable iff the labeled statement is reachable.
-
An expression statement can complete normally iff it is reachable.
-
An
if-thenstatement can complete normally iff it is reachable.The
then-statement is reachable iff theif-thenstatement is reachable.An
if-then-elsestatement can complete normally iff thethen-statement can complete normally or theelse-statement can complete normally.The
then-statement is reachable iff theif-then-elsestatement is reachable.The
else-statement is reachable iff theif-then-elsestatement is reachable.This handling of an
ifstatement, whether or not it has anelsepart, is rather unusual. The rationale is given at the end of this section. -
An
assertstatement can complete normally iff it is reachable. -
A
switchstatement can complete normally iff at least one of the following is true:- The switch block is empty or contains only switch labels.
- The last statement in the switch block can complete normally.
- There is at least one switch label after the last switch block statement group.
- The switch block does not contain a
defaultlabel. - There is a reachable
breakstatement that exits theswitchstatement.
-
A switch block is reachable iff its
switchstatement is reachable. -
A statement in a switch block is reachable iff its
switchstatement is reachable and at least one of the following is true:- It bears a
caseordefaultlabel. - There is a statement preceding it in the switch block and that preceding statement can complete normally.
- It bears a
-
A
whilestatement can complete normally iff at least one of the following is true:- The
whilestatement is reachable and the condition expression is not a constant expression (§15.28) with valuetrue. - There is a reachable
breakstatement that exits thewhilestatement.
The contained statement is reachable iff the
whilestatement is reachable and the condition expression is not a constant expression whose value isfalse. - The
-
A
dostatement can complete normally iff at least one of the following is true:- The contained statement can complete normally and the condition expression is not a constant expression (§15.28) with value
true. - The
dostatement contains a reachablecontinuestatement with no label, and thedostatement is the innermostwhile,do, orforstatement that contains thatcontinuestatement, and thecontinuestatement continues thatdostatement, and the condition expression is not a constant expression with valuetrue. - The
dostatement contains a reachablecontinuestatement with a labelL, and thedostatement has labelL, and thecontinuestatement continues thatdostatement, and the condition expression is not a constant expression with valuetrue. - There is a reachable
breakstatement that exits thedostatement.
The contained statement is reachable iff the
dostatement is reachable. - The contained statement can complete normally and the condition expression is not a constant expression (§15.28) with value
-
A basic
forstatement can complete normally iff at least one of the following is true:- The
forstatement is reachable, there is a condition expression, and the condition expression is not a constant expression (§15.28) with valuetrue. - There is a reachable
breakstatement that exits theforstatement.
The contained statement is reachable iff the
forstatement is reachable and the condition expression is not a constant expression whose value isfalse. - The
-
An enhanced
forstatement can complete normally iff it is reachable. -
A
break,continue,return, orthrowstatement cannot complete normally. -
A
synchronizedstatement can complete normally iff the contained statement can complete normally.The contained statement is reachable iff the
synchronizedstatement is reachable. -
A
trystatement can complete normally iff both of the following are true:- The
tryblock can complete normally or anycatchblock can complete normally. - If the
trystatement has afinallyblock, then thefinallyblock can complete normally.
- The
-
The
tryblock is reachable iff thetrystatement is reachable. -
A
catchblockCis reachable iff both of the following are true:-
Either the type of
C's parameter is an unchecked exception type orExceptionor a superclass ofException, or some expression orthrowstatement in thetryblock is reachable and can throw a checked exception whose type is assignable to the type ofC's parameter. (An expression is reachable iff the innermost statement containing it is reachable.)See §15.6 for normal and abrupt completion of expressions.
-
There is no earlier
catchblockAin thetrystatement such that the type ofC's parameter is the same as or a subclass of the type ofA's parameter.
-
-
The Block of a
catchblock is reachable iff thecatchblock is reachable. -
If a
finallyblock is present, it is reachable iff thetrystatement is reachable.
One might expect the if statement to be handled in the following manner:
-
An
if-thenstatement can complete normally iff at least one of the following is true:- The
if-thenstatement is reachable and the condition expression is not a constant expression whose value istrue. - The
then-statement can complete normally.
The
then-statement is reachable iff theif-thenstatement is reachable and the condition expression is not a constant expression whose value isfalse. - The
-
An
if-then-elsestatement can complete normally iff thethen-statement can complete normally or theelse-statement can complete normally.The
then-statement is reachable iff theif-then-elsestatement is reachable and the condition expression is not a constant expression whose value isfalse.The
else-statement is reachable iff theif-then-elsestatement is reachable and the condition expression is not a constant expression whose value istrue.
This approach would be consistent with the treatment of other control structures. However, in order to allow the if statement to be used conveniently for "conditional compilation" purposes, the actual rules differ.
As an example, the following statement results in a compile-time error:
while (false) { x=3; }
because the statement x=3; is not reachable; but the superficially similar case:
if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.
The rationale for this differing treatment is to allow programmers to define "flag" variables such as:
static final boolean DEBUG = false;
and then write code such as:
if (DEBUG) { x=3; }
The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
Conditional compilation comes with a caveat. If a set of classes that use a "flag" variable - or more precisely, any static constant variable (§4.12.4) - are compiled and conditional code is omitted, it does not suffice later to distribute just a new version of the class or interface that contains the definition of the flag. The classes that use the flag will not see its new value, so their behavior may be surprising. In essence, a change to the value of a flag is binary compatible with pre-existing binaries (no LinkageError occurs) but not behaviorally compatible.
Another reason for "inlining" values of static constant variables is because of switch statements. They are the only kind of statement that relies on constant expressions, namely that each case label of a switch statement must be a constant expression whose value is different than every other case label. case labels are often references to static constant variables so it may not be immediately obvious that all the labels have different values. If it is proven that there are no duplicate labels at compile time, then inlining the values into the class file ensures there are no duplicate labels at run time either - a very desirable property.
Example 14.21-1. Conditional Compilation
If the example:
class Flags { static final boolean DEBUG = true; }
class Test {
public static void main(String[] args) {
if (Flags.DEBUG)
System.out.println("DEBUG is true");
}
}
is compiled and executed, it produces the output:
DEBUG is true
Suppose that a new version of class Flags is produced:
class Flags { static final boolean DEBUG = false; }
If Flags is recompiled but not Test, then running the new binary with the existing binary of Test produces the output:
DEBUG is true
because DEBUG is a static constant variable, so its value could have been used in compiling Test without making a reference to the class Flags.
This behavior would also occur if Flags was an interface, as in the modified example:
interface Flags { boolean DEBUG = true; }
class Test {
public static void main(String[] args) {
if (Flags.DEBUG)
System.out.println("DEBUG is true");
}
}
In fact, because the fields of interfaces are always static and final, we recommend that only constant expressions be assigned to fields of interfaces. We note, but do not recommend, that if a field of primitive type of an interface may change, its value may be expressed idiomatically as in:
interface Flags {
boolean debug = Boolean.valueOf(true).booleanValue();
}
ensuring that this value is not a constant expression. Similar idioms exist for the other primitive types.