Variable Scope
in Java
Learn how Java determines where a variable can be used, why scope matters for writing correct programs, and how block scope shapes your code.
Java
AP Computer Science A
What Is Variable Scope?
Every variable you declare in Java has a scope: the region of your code where that variable exists and can be accessed. Outside its scope, the variable is invisible: the compiler doesn't know it exists.
Why Does Scope Matter?
✓ Prevents Bugs
Variables are confined to where they are needed, reducing accidental changes from distant code.
✓ Saves Memory
Java reclaims memory for a variable as soon as its scope ends, keeping your program efficient.
✓ Improves Readability
Declaring variables close to where they are used makes code easier to follow and maintain.
Visualizing Scope as Nested Boxes
The Fundamental Rule
1
Declaration point: the variable does not exist on lines before the line where it is declared.
2
Enclosing braces: the nearest pair of { } surrounding the declaration defines the boundary.
3
Closing brace: the variable is destroyed when execution passes the } that ends its block.
Block Scope in Detail
Local vs Instance vs Block
Local Variable
Declared inside a method. Lives from declaration to the method's closing }.
Instance Variable
Declared inside a class but outside any method. Lives as long as the object exists.
Block Variable
Declared inside if, for, while, or any { } block. Dies at that block's }.
How to Determine Scope
1
Find the declaration: locate the line where the variable is first declared (type + name).
2
Look for the nearest enclosing { }: identify the innermost pair of braces that surrounds the declaration.
3
Mark the boundaries: the variable is accessible from the declaration line down to the closing } of that block.
4
Check access points: any line that uses the variable must fall within those boundaries. If not, it is a compile error.
Example 1: for-loop Scope
for (int i = 0; i < 5; i++) {
System.out.println(i); // ✓ i is in scope
}
System.out.println(i); // ✕ COMPILE ERROR
Example 2: Nested Blocks
public void calculate() {
int result = 0;
for (int i = 0; i < 3; i++) {
int temp = i * 2;
result += temp; // ✓ result visible here
}
System.out.println(result); // ✓ result still alive
System.out.println(temp); // ✕ COMPILE ERROR
System.out.println(i); // ✕ COMPILE ERROR
}
Example 3: Shadowing and Scope
public class Demo {
int value = 100; // instance variable
public void show() {
int value = 50; // local variable "shadows" instance
System.out.println(value); // prints 50
System.out.println(this.value); // prints 100
}
}
Method Parameters Are Local
public void greet(String name) {
System.out.println("Hello, " + name);
}
public void other() {
System.out.println(name); // ✕ COMPILE ERROR
}
Scope Lifetime Timeline
Common Mistakes
✕ Using Before Declaring
Referencing a variable on a line above its declaration. Java reads top-to-bottom: the variable does not exist yet.
✕ Accessing Outside Block
Trying to use a loop or if-block variable after the closing }. The variable has already been destroyed.
✕ Unintended Shadowing
Declaring a local variable with the same name as an instance variable, then wondering why the instance value did not change.
Scope Comparison Table
| Instance | Local | Block | |
|---|---|---|---|
| Declared In | Class body | Method body | if / for / while / { } |
| Lifetime | Object lifetime | Method call | Block execution |
| Default Value | Yes (0, null, false) | No | No |
| Visible To | All methods in class | Only that method | Only that block |
| Can Shadow? | N/A | Can shadow instance | Can shadow local |
Question 1 of 5
public void test() {
int a = 5;
if (a > 0) {
int b = 10;
}
System.out.println(b);
}
▼ Tap to read code
What happens when this code is compiled?
The variable b is declared inside the if-block. Its scope ends at the closing } of that block (line 5). Line 6 is outside that scope, so the compiler cannot find b.
Question 2 of 5
A variable declared in a for-loop header (e.g., int i = 0) is accessible:
A for-loop header variable's scope includes both the header (condition, update) and the body. It ends at the loop's closing }.
Question 3 of 5
public class Box {
int size = 20;
public void resize() {
int size = 50;
System.out.println(size);
}
}
▼ Tap to read code
What does resize() print?
The local variable size (value 50) shadows the instance variable size (value 20). Inside the method, the unqualified name size refers to the local variable. To access 20, you would need this.size.
Question 4 of 5
Which statement about instance variables is true?
Instance variables receive default values (0 for int, null for objects, false for boolean) and persist for the lifetime of the object. Every non-static method in the class can access them.
Question 5 of 5
public void mystery() {
for (int i = 0; i < 3; i++) {
int val = i * 10;
}
for (int i = 0; i < 3; i++) {
System.out.println(i);
}
}
▼ Tap to read code
Does this code compile? If so, what does it print?
Each for-loop creates its own scope. The first i and val are destroyed after loop 1 ends. The second loop declares a new i in a separate scope: no conflict. It prints 0, 1, 2.