Java Reference
In-Depth Information
if (flag)
k = 3;
if (!flag)
k = 4;
System.out.println(k); /* k is not "definitely assigned"
before this statement */
}
and so compiling this program must cause a compile-time error to occur.
Example 16-3. Definite Unassignment
A Java compiler will accept the code:
void unflow(boolean flag) {
final int k;
if (flag) {
k = 3;
System.out.println(k);
}
else {
k = 4;
System.out.println(k);
}
}
as far as definite unassignment of k is concerned, because the rules outlined in this
section allow it to tell that k is assigned at most once (indeed, exactly once) no matter
whether the flag is true or false . But the rules do not accept the variation:
Click here to view code image
void unflow(boolean flag) {
final int k;
if (flag) {
k = 3;
System.out.println(k);
}
if (!flag) {
k = 4;
System.out.println(k); /* k is not "definitely unassigned"
before this statement */
}
}
and so compiling this program must cause a compile-time error to occur.
Search WWH ::




Custom Search