The complier error is because the rules of the language doesn't make a clear point on the scope of the variable declaration of "items".
For example, if I have a block of code like this:
bool isTrue() {
bool returnValue = false;
if (cheese.isGreen()) {
returnValue = true;
}
return returnValue;
}
it is painfully clear that returnValue is a valid variable across the entire method.
If I have a block of code like this:
bool isTrue() {
if (cheese.isGreen()) {
bool returnValue = true;
}
return returnValue;
}
it is painfully clear that returnValue is not valid outside of the "if clause".
But if I have a block of code like this:
bool isTrue() {
if (cheese.isGreen())
bool returnValue = true;
return returnValue;
}
it is not clear if returnValue is within the scope of the if statement or if returnValue is within the scope of the entire method. This is due to the details of the layout of the Java language grammar. Basically, it's allowed to declare new variables within any block (because a block clearly defines scope of the variable) but this if statement doesn't contain a block.
If you assume that Java adds the block for you silently, then the scope is within the "forgotten block". If you assume that since there's not explicit block to contain the scope, then the scope of the variable is at the same level as the rest of the method. Arguments abound as to which viewpoint is "more" correct, so the entire attempt to do such a thing is forbidden.
If this seems odd to you, and you thing that only a crazy man would not assume that the variable is defined within the scope of an implied block, please remember that there were languages prior to Java where the scope would have been at the same level as the return statement. Yes, crazy languages by today's standards, but they still existed.