Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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".</p> <p>For example, if I have a block of code like this:</p> <pre><code>bool isTrue() { bool returnValue = false; if (cheese.isGreen()) { returnValue = true; } return returnValue; } </code></pre> <p>it is painfully clear that returnValue is a valid variable across the entire method.</p> <p>If I have a block of code like this:</p> <pre><code>bool isTrue() { if (cheese.isGreen()) { bool returnValue = true; } return returnValue; } </code></pre> <p>it is painfully clear that returnValue is not valid outside of the "if clause".</p> <p>But if I have a block of code like this:</p> <pre><code>bool isTrue() { if (cheese.isGreen()) bool returnValue = true; return returnValue; } </code></pre> <p>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.</p> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload