PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
In this method call, values is the parameter, while total, i, current, and answer are local variables declared inside the method. Each declaration becomes usable at its own line, and its enclosing braces determine where it stops being usable. Being written somewhere inside the same method is not enough to make a name available everywhere in that method.
static int sumBeforeSeven(int[] values) {
int total = 0;
for (int i = 0; i < values.length; i++) {
int current = values[i];
if (current == 7) {
int answer = total;
return answer;
}
total += current;
}
return total;
}
int result = sumBeforeSeven(new int[]{4, 2, 7, 1}); // 6The declaration of answer is inside the braces belonging to if (current == 7). You can reference answer in return answer;, but not after that if block. Likewise, current can be used by the if statement and by total += current;, but not after the loop body's closing brace. The braces do not merely organize indentation, they define boundaries that Java checks.
Where can answer be legally referenced in the exact sumBeforeSeven method?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The loop body executes once for each visited array element, but current is not one permanent variable whose value is merely changed forever. Each execution enters the loop-body block and creates a fresh current from that iteration's array value. In the call with [4, 2, 7, 1], the first three iterations create current with 4, then 2, then 7.
iteration current total before action
1 4 0 total becomes 4
2 2 4 total becomes 6
3 7 6 answer becomes 6, then return 6On the first iteration, current is 4, so the if condition is false and total becomes 4. The second iteration creates a new current with 2, then total becomes 6. The third iteration creates another new current with 7, so answer is created with the current total, 6. The method returns before the value 1 is used.
A local declaration alone does not give total a value. If you change the first line to int total;, the later compound assignment becomes a problem: total += current must first read total, add current to it, and write the result back. Java cannot prove that total has a value at that point, so it rejects the method during compilation.
static int sumBeforeSeven(int[] values) {
int total;
for (int i = 0; i < values.length; i++) {
int current = values[i];
if (current == 7) {
int answer = total;
return answer;
}
total += current;
}
return total;
}The same issue appears at int answer = total;, because answer also needs total to already hold a value when the 7 is found. Restore the explicit initialization int total = 0;. That assignment gives total a definite starting value before the first loop iteration, so every later read has a valid path from an assignment, and the call returns 6.
In this version, type the shortest edit that makes the first read of total legal and preserves the returned result 6.
static int sumBeforeSeven(int[] values) {
int total;
for (int i = 0; i < values.length; i++) {
int current = values[i];
if (current == 7) {
int answer = total;
return answer;
}
total += current;
}
return total;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
When the third iteration creates current with 7, total already contains 6. The declaration int answer = total; copies that integer value into answer, and return answer sends the value 6 out of the method. The caller receives the returned integer, not access to answer, total, i, or current.
current = 7
total = 6
answer = 6
return answer
caller receives: 6After return runs, the method call is finished and its local variables no longer exist. If sumBeforeSeven is called again with the same array, the new call creates a new total starting at 0, a new loop variable i, and new current variables as its loop body executes. If it reaches 7, it also creates a new answer. Nothing from the earlier call is retained by these locals.