Variable scope in Java is the region of code where a variable can be used. Java has three kinds of variables with different scopes: local variables live inside a method or block, instance variables belong to one object, and class (static) variables are shared by every object of the class.
Understanding variable scope java rules explains most "cannot find symbol" errors and many subtle bugs, such as a loop counter you cannot read after the loop. This article builds on declaring variables and data types.
| Kind | Declared | Lives as long as | Default value | Accessed through |
|---|---|---|---|---|
| Local variable | Inside a method, constructor or block | The block runs | None, must be assigned | Its name, inside its block only |
| Instance variable | In the class, without static | The object exists | 0, false or null | An object reference |
| Class variable | In the class, with static | The class is loaded | 0, false or null | The class name or an object |
The scope of a variable is also tied to its lifetime. Locals are stored on the stack frame of the method call and disappear when the method returns. Instance variables are stored with the object on the heap. Static variables belong to the class itself and exist once per class loader.
A local variable is visible from its declaration to the closing brace of the block that contains it. A block is any code between { and }: a method body, an if branch, a loop body, or a plain pair of braces.
total is declared in the method body, so it is visible inside the loop and after it. i and square are declared inside the for statement and its body, so they disappear when the loop ends. If you need a value after the loop, declare it before the loop.
Unlike fields, local variables get no default value. The compiler performs definite assignment analysis and rejects reads of a variable that might not have been assigned.
Java does not allow a local variable to reuse the name of another local variable that is still in scope, including method parameters.
Two sibling blocks may reuse the same name because their scopes do not overlap.
An instance variable is declared in a class but outside any method, without static. Every object gets its own copy, and methods of the class can use it by name.
Each Counter has its own count, so a and b do not affect each other. Instance variables are visible throughout the class, and access from other classes depends on the access modifier (private, package-private, protected, public).
A static variable is shared by all instances and exists even when no object has been created.
Reach a static variable through the class name (Visitor.totalVisitors) rather than through an object, so readers can see that it is shared. A static method cannot use instance variables directly, because it has no object; it can only use static members and its own locals.
A local variable or parameter may have the same name as a field. Inside that method the local variable shadows the field, and this.name reaches the field.
Without this., the line name = name; would assign the parameter to itself and leave the field as null. Constructors and setters use this pattern constantly.
Lambda expressions and anonymous classes can read local variables of the enclosing method, but only if they are final or effectively final, meaning the variable is never reassigned after its first assignment.
A lambda body also cannot declare a local variable with the same name as a local in the enclosing method. Fields have no such restriction, because they live on the object, not on the stack.
It is the loop only. A variable declared in the initialiser, such as int i, is visible in the condition, the update and the body, and nowhere after the loop.
No. Fields get default values: 0 for numeric types, false for boolean and null for references. Local variables have no defaults and must be assigned first.
Not directly, because it runs without an object. Create or receive an object and use obj.field, or make the field static if it really is shared.
Scope is where in the source code a name can be used. Lifetime is how long the variable exists at runtime. A local's lifetime matches its block, while a field lives as long as its object or class.
Local variables exist only in the block that declares them, must be assigned before use and cannot shadow other locals. Instance variables belong to each object and get default values. Static variables belong to the class and are shared. Use this when a parameter shadows a field, keep variables in the smallest scope that works, and remember that lambdas can capture only effectively final locals.