Variable Scope
What Is Scope?
Scope is the region of code where a variable is visible and can be used. Once you leave that region, the variable no longer exists (or, for fields, is no longer accessible without going through an object or class reference). Java determines scope primarily by where a variable is declared — inside a block, a method, a class, or with the static keyword.
Block Scope
Any pair of curly braces { } introduces a block, and a variable declared inside a block is only visible from its declaration point to the closing brace of that block. This applies to if statements, loops, and any standalone { } block.
Block scope
public class BlockScopeDemo {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
int squared = i * i; // squared only exists inside this loop body
System.out.println(squared);
}
// System.out.println(squared); // compile error: squared is out of scope here
// System.out.println(i); // compile error: i is out of scope here
}
}0 1 4
Method-Local Variables
Variables declared directly inside a method body — including its parameters — are local variables. They are created when the method is called and destroyed when it returns; each call gets its own independent copy.
Method-local scope
public class LocalScopeDemo {
static int square(int n) { // n is local to square()
int result = n * n; // result is also local to square()
return result;
}
public static void main(String[] args) {
System.out.println(square(5));
// n and result from square() are not visible here
}
}Instance Variables (Fields)
A variable declared directly inside a class, but outside any method, is an instance variable, or field. It is scoped to the object — every instance of the class gets its own copy, and the field is visible from every non-static method in the class.
Instance variable scope
public class Counter {
private int count; // instance variable, one per Counter object
void increment() {
count++; // visible throughout the class
}
int getCount() {
return count;
}
}Class (Static) Variables
A field declared with the static keyword belongs to the class itself rather than to any one object. It is scoped to the class as a whole — there is exactly one copy, shared by every instance, and it can even be accessed without creating an object at all.
Static variable scope
public class Counter {
static int totalCounters = 0; // shared across ALL Counter objects
private int count;
Counter() {
totalCounters++; // every new Counter increments the shared total
}
}Comparing the Four Kinds of Scope
Kind | Declared | Visible In | Lifetime |
Block-scoped | Inside { } | That block only | While the block executes |
Method-local | Inside a method body / parameters | That method only | While the method call is on the stack |
Instance variable | In a class, no static | Any non-static method of the class (via the object) | As long as the object exists |
Static (class) variable | In a class, with static | Anywhere the class is accessible | As long as the class is loaded |
Variable Shadowing
Shadowing happens when a local variable or method parameter has the same name as an instance field. Inside that method, the local name wins — plain use of the identifier refers to the local variable or parameter, and the field is temporarily hidden.
A shadowed field
public class Person {
private String name;
void setName(String name) { // parameter "name" shadows the field "name"
name = name; // does nothing useful: assigns the parameter to itself!
}
}Keep variable scope as narrow as possible — declare variables close to where they are used
Avoid reusing a field's name for a local variable or parameter unless you specifically intend to shadow it
When you do shadow a field on purpose (very common in constructors and setters), use this.fieldName to disambiguate