Java Scope
A variable's scope is the region of code where it can be used. In Java, scope is determined by where the variable is declared — and the rule of thumb is simple: a variable lives inside the nearest pair of curly braces { } that surrounds its declaration.
Method scope
A variable declared inside a method is a local variable. It can be used anywhere in that method after the line where it's declared, and it disappears when the method returns:
class Main {
static void showTotal() {
int total = 40; // local to showTotal
System.out.println("Total: " + total);
}
public static void main(String[] args) {
// System.out.println(total); // error: total doesn't exist here
int total = 99; // a different variable, local to main
showTotal();
System.out.println("main's total: " + total);
}
}Total: 40
main's total: 99
The two total variables have nothing to do with each other. Parameters behave like local variables too: they exist only inside their method.
Block scope
Any { } block — the body of an if, a loop, or even a bare pair of braces — creates a new, smaller scope. Variables declared inside are invisible outside:
class Main {
public static void main(String[] args) {
int x = 10;
if (x > 5) {
int doubled = x * 2; // only exists inside this if
System.out.println("doubled = " + doubled);
}
// System.out.println(doubled); // error: cannot find symbol
{
String note = "inside a bare block";
System.out.println(note);
}
System.out.println("x is still visible: " + x);
}
}doubled = 20
inside a bare block
x is still visible: 10
Inner blocks can see variables from the outer blocks, but not the other way round.
Loop scope
A variable declared in a for header belongs to the loop. If you need the value after the loop ends, declare the variable before the loop:
class Main {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
}
System.out.println();
// System.out.println(i); // error: i is out of scope
int found = -1;
int[] data = {4, 9, 2, 9};
for (int i = 0; i < data.length; i++) { // a fresh i - allowed
if (data[i] == 9) {
found = i;
break;
}
}
System.out.println("First 9 at index " + found);
}
}0 1 2
First 9 at index 1
No shadowing of locals
Java doesn't allow a local variable to be redeclared in an inner block while the outer one is still in scope — it would be too easy to confuse the two:
int count = 0;
for (int i = 0; i < 3; i++) {
int count = i; // error: variable count is already defined in method main
}Class scope (fields)
Variables declared directly in a class, outside any method, are fields. They're visible in every method of the class. A static field is shared by all static methods — handy for a counter:
class Main {
static int callCount = 0; // field: class scope
static void doWork() {
callCount++;
int local = callCount * 10; // recreated on every call
System.out.println("call " + callCount + ", local " + local);
}
public static void main(String[] args) {
doWork();
doWork();
doWork();
System.out.println("Total calls: " + callCount);
}
}call 1, local 10
call 2, local 20
call 3, local 30
Total calls: 3
The field keeps its value between calls; the local variable starts fresh each time. A local variable may have the same name as a field — it then hides (shadows) the field inside that method. You'll see how this.name resolves that in the Classes section.
| Declared in | Visible in | Lives for |
|---|---|---|
a block { } / loop header |
that block only | until the block ends |
| a method (or as a parameter) | the rest of the method | one method call |
| a class (field) | all methods of the class | as long as the object / class exists |
Tip: Declare variables in the smallest scope that works, as close as possible to where they're used. It makes code easier to read and prevents accidental reuse.
Test yourself
Where can a variable declared inside an if block be used?
- Anywhere in the method
- Anywhere in the class
- Only inside that if block
- Only before the if
Why does for (int i = 0; i < 3; i++) {} System.out.println(i); fail?
- i is final
- i is out of scope after the loop
- println can't print ints
- The loop never runs
Which kind of variable keeps its value between method calls?
- A local variable
- A parameter
- A field
- A loop variable