16.2.1. Empty Statements
- V is [un]assigned after an empty statement (§14.6) iff it is [un]assigned before the empty statement.
16.2.2. Blocks
-
A blank
finalmember field V is definitely assigned (and moreover is not definitely unassigned) before the block (§14.2) 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 is definitely unassigned (and moreover is not definitely assigned) before the block that is the body of the constructor, method, instance initializer or static initializer that declares V.
-
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 constructor, 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 constructor, 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 constructor, 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
ein 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^=ethat 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 Declaration Statements
- V is [un]assigned after a local class declaration statement (§14.3) iff V is [un]assigned before the local class declaration statement.
16.2.4. Local Variable Declaration Statements
- V is [un]assigned after a local variable declaration statement (§14.4) 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
eother than the first one in the local variable declaration statement iff either V is definitely assigned after the variable initializer to the left ofeor the initializer expression to the left ofeis in the declarator that declares V. - V is definitely unassigned before any variable initializer
eother than the first one in the local variable declaration statement iff V is definitely unassigned after the variable initializer to the left ofeand the initializer expression to the left ofeis not in the declarator that declares V.
16.2.5. Labeled Statements
- V is [un]assigned after a labeled statement
L:S (whereLis a label) (§14.7) iff V is [un]assigned after S and V is [un]assigned before everybreakstatement that may exit the labeled statementL: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) iff it is [un]assigned aftere. - V is [un]assigned before
eiff it is [un]assigned beforee;.
16.2.7. if Statements
The following rules apply to a statement if (e) S (§14.9.1):
- V is [un]assigned after
if (e) Siff V is [un]assigned after S and V is [un]assigned afterewhen false. - V is [un]assigned before
eiff V is [un]assigned beforeif (e) S. - V is [un]assigned before S iff V is [un]assigned after
ewhen true.
The following rules apply to a statement if (e) S else T (§14.9.2):
- V is [un]assigned after
if (e) S else Tiff V is [un]assigned after S and V is [un]assigned after T. - V is [un]assigned before
eiff V is [un]assigned beforeif (e) S else T. - V is [un]assigned before S iff V is [un]assigned after
ewhen true. - V is [un]assigned before T iff V is [un]assigned after
ewhen 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):
- V is [un]assigned before
e_1iff V is [un]assigned before theassertstatement. - V is definitely assigned after the
assertstatement iff V is definitely assigned before theassertstatement. - V is definitely unassigned after the
assertstatement iff V is definitely unassigned before theassertstatement and V is definitely unassigned aftere_1when true.
The following rule applies to a statement assert e_1 : e_2 :
- V is [un]assigned before
e_2iff V is [un]assigned aftere_1when false.
16.2.9. switch Statements
-
V is [un]assigned after a
switchstatement (§14.11) iff all of the following are true:- Either there is a
defaultlabel in the switch block or V is [un]assigned after the switch expression. - Either there are no switch labels in the switch block that do not begin a block-statement-group (that is, there are no switch labels immediately before the "
}" that ends the switch block) or V is [un]assigned after the switch expression. - Either the switch block contains no block-statement-groups or V is [un]assigned after the last block-statement of the last block-statement-group.
- V is [un]assigned before every
breakstatement that may exit theswitchstatement.
- Either there is a
-
V is [un]assigned before the switch expression iff V is [un]assigned before the
switchstatement.
If a switch block contains at least one block-statement-group, then the following rules also apply:
- V is [un]assigned before the first block-statement of the first block-statement-group in the switch block iff V is [un]assigned after the switch expression.
- V is [un]assigned before the first block-statement of any block-statement-group other than the first iff V is [un]assigned after the switch expression and V is [un]assigned after the preceding block-statement.
16.2.10. while Statements
-
V is [un]assigned after
while (e) S(§14.12) iff V is [un]assigned afterewhen false and V is [un]assigned before everybreakstatement for which thewhilestatement is the break target. -
V is definitely assigned before
eiff V is definitely assigned before thewhilestatement. -
V is definitely unassigned before
eiff all of the following are true:- V is definitely unassigned before the
whilestatement. - 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 everycontinuestatement for which thewhilestatement is the continue target.
- V is definitely unassigned before the
-
V is [un]assigned before S iff V is [un]assigned after
ewhen true.
16.2.11. do Statements
-
V is [un]assigned after
do S while (e);(§14.13) iff V is [un]assigned afterewhen false and V is [un]assigned before everybreakstatement for which thedostatement is the break target. -
V is definitely assigned before S iff V is definitely assigned before the
dostatement. -
V is definitely unassigned before S iff all of the following are true:
- V is definitely unassigned before the
dostatement. - Assuming V is definitely unassigned before S, V is definitely unassigned after
ewhen true.
- V is definitely unassigned before the
-
V is [un]assigned before
eiff V is [un]assigned after S and V is [un]assigned before everycontinuestatement for which thedostatement is the continue target.
16.2.12. for Statements
The rules herein cover the basic for statement (§14.14.1). Since the enhanced for statement (§14.14.2) is defined by translation to a basic for statement, no special rules need to be provided for it.
-
V is [un]assigned after a
forstatement 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
breakstatement for which theforstatement is the break target.
-
V is [un]assigned before the initialization part of the
forstatement iff V is [un]assigned before theforstatement. -
V is definitely assigned before the condition part of the
forstatement iff V is definitely assigned after the initialization part of theforstatement. -
V is definitely unassigned before the condition part of the
forstatement iff all of the following are true:- V is definitely unassigned after the initialization part of the
forstatement. - Assuming V is definitely unassigned before the condition part of the
forstatement, V is definitely unassigned after the contained statement. - Assuming V is definitely unassigned before the contained statement, V is definitely unassigned before every
continuestatement for which theforstatement is the continue target.
- V is definitely unassigned after the initialization part of the
-
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 after the initialization part of the
forstatement.
-
V is [un]assigned before the incrementation part of the
forstatement iff V is [un]assigned after the contained statement and V is [un]assigned before everycontinuestatement for which theforstatement is the continue target.
16.2.12.1. Initialization Part of for Statement
-
If the initialization part of the
forstatement is a local variable declaration statement, the rules of §16.2.4 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
forstatement 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, continue, return, and throw Statements
-
By convention, we say that V is [un]assigned after any
break,continue,return, orthrowstatement (§14.15, §14.16, §14.17, §14.18).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,continue,return, orthrowstatement never completes normally, it vacuously satisfies this notion. -
In a
returnstatement with an expressioneor athrowstatement with an expressione, V is [un]assigned beforeeiff V is [un]assigned before thereturnorthrowstatement.
16.2.14. synchronized Statements
- V is [un]assigned after
synchronized (e) S(§14.19) iff V is [un]assigned after S. - V is [un]assigned before
eiff V is [un]assigned before the statementsynchronized (e) S. - V is [un]assigned before S iff V is [un]assigned after
e.
16.2.15. try Statements
These rules apply to every try statement (§14.20), whether or not it has a finally block:
-
V is [un]assigned before the
tryblock iff V is [un]assigned before thetrystatement. -
V is definitely assigned before a
catchblock iff V is definitely assigned before thetryblock. -
V is definitely unassigned before a
catchblock iff all of the following are true:- V is definitely unassigned after the
tryblock. - V is definitely unassigned before every
returnstatement that belongs to thetryblock. - V is definitely unassigned after
ein every statement of the formthrowethat belongs to thetryblock. - V is definitely unassigned after every
assertstatement that occurs in thetryblock. - V is definitely unassigned before every
breakstatement that belongs to thetryblock and whose break target contains (or is) thetrystatement. - V is definitely unassigned before every
continuestatement that belongs to thetryblock and whose continue target contains thetrystatement.
- V is definitely unassigned after the
If a try statement does not have a finally block, then this rule also applies:
- V is [un]assigned after the
trystatement iff V is [un]assigned after thetryblock and V is [un]assigned after everycatchblock in thetrystatement.
If a try statement does have a finally block, then these rules also apply:
-
V is definitely assigned after the
trystatement iff at least one of the following is true:- V is definitely assigned after the
tryblock and V is definitely assigned after everycatchblock in thetrystatement. - V is definitely assigned after the
finallyblock. - V is definitely unassigned after a
trystatement iff V is definitely unassigned after thefinallyblock.
- V is definitely assigned after the
-
V is definitely assigned before the
finallyblock iff V is definitely assigned before thetrystatement. -
V is definitely unassigned before the
finallyblock iff all of the following are true:- V is definitely unassigned after the
tryblock. - V is definitely unassigned before every
returnstatement that belongs to thetryblock. - V is definitely unassigned after
ein every statement of the formthrowethat belongs to thetryblock. - V is definitely unassigned after every
assertstatement that occurs in thetryblock. - V is definitely unassigned before every
breakstatement that belongs to thetryblock and whose break target contains (or is) thetrystatement. - V is definitely unassigned before every
continuestatement that belongs to thetryblock and whose continue target contains thetrystatement. - V is definitely unassigned after every
catchblock of thetrystatement.
- V is definitely unassigned after the