DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSJAVA FUNDAMENTALS

Local Variables End Where Their Braces End

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 25

A local variable is usable only from its declaration to the end of its block

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.

JAVAThe parameter values enters the method from the call. The other four names are declared inside nested blocks.
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}); // 6
  • total starts at its declaration and remains usable until the method body's closing brace.
  • i starts in the for statement and is usable through that for statement, including its loop body.
  • current starts inside the loop body and stops being usable at the loop body's closing brace.
  • answer starts inside the if block and stops being usable at the if block's closing brace.

The 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.

The nested scopes in sumBeforeSeven for the array [4, 2, 7, 1]A nested scope diagram of sumBeforeSeven shows the method body as the outer region, with total beginning at `int total = 0;` and continuing to the method's closing brace. Inside it, the for statement contains i. The loop-body braces contain current beginning at `int current = values[i];`. The if-block braces form the smallest region, where answer begins at `int answer = total;` and remains usable through `return answer;` but not after that block.int total = 0;int i = 0;int current;int answer = total;return answer;method-body scope · sumBeforeSevenloop scope · for statement + bodyloop-body scopeif-block scope · current == 7Each declaration starts its scope; the nearest enclosing braces end it.
The nearest enclosing braces set the end of each local variable's scope.
CHECKPOINT 1Not answered

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.

Executing a block again creates fresh local variables

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.

TEXTThe call stops when the third loop iteration sees 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 6

On 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.

Java rejects a read before a local variable has definitely been assigned

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.

JAVAThis version fails because total is read before Java can prove it was assigned.
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.

CHECKPOINT 2Not answered

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.

Returning from the method discards every local variable from that call

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.

TEXTThe return transfers the integer value, not the local variable itself.
current = 7
 total = 6
 answer = 6
 return answer

caller receives: 6

After 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.

Previous · Standard Library MethodsNext part · Quiz