01 / 20

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

CLASS SCOPE int instanceVar = 10; METHOD SCOPE int localVar = 20; BLOCK SCOPE (if) int blockVar = 30; BLOCK SCOPE (for) int i = 0;

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

1 2 3 4 5 6 7 8 9 10 11 public void demo() { int x = 5; if (x > 0) { int y = 10; System.out.println(x + y); } // y is NOT accessible here System.out.println(x); } x y

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

Program Execution Time → method { } if { } Instance Variable (object lifetime) Local Variable (method lifetime) Block Variable born dies born dies

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

What happens when this code is compiled?

Question 2 of 5

A variable declared in a for-loop header (e.g., int i = 0) is accessible:

Question 3 of 5

What does resize() print?

Question 4 of 5

Which statement about instance variables is true?

Question 5 of 5

Does this code compile? If so, what does it print?