Sub topic 9
Scope and Shadowing
A variable stores a value, but you cannot use its name everywhere in a Java program. Its scope is the part of the program where that name is available. Knowing the scope helps you avoid using a name where Java cannot find it.
This chapter assumes you can read a basic Java class and its main method. A method is a named group of instructions.
Blocks and local scope
A block is a group of instructions inside curly braces. A local variable is a variable declared inside a method or one of its blocks. Its scope runs from its declaration to the end of that block.
An inner block can use local variables from an outer block, as long as those variables are already in scope. But a variable declared in the inner block is not available after that block ends.
A method parameter is a variable that receives a value when the method is called. A parameter is in scope throughout the method body.
Fields and shadowing
An object is an instance of a class. A field is a variable that belongs to an object. A local variable can have the same name as a field. When that happens, the local variable shadows the field: using the name by itself refers to the local variable.
Inside an object’s method, you can use this followed by a dot and the name to reach the field instead. In this case, this means the current object.
Worked example
Save this program as ScopeExample.java:
class ScopeExample {
int score = 10;
void show() {
int score = 3;
System.out.println(score);
System.out.println(this.score);
for (int step = 1; step <= 2; step++) {
System.out.println(step);
}
// System.out.println(step);
System.out.println(score);
}
public static void main(String[] args) {
ScopeExample example = new ScopeExample();
example.show();
}
}Run these commands in the folder where you saved the file. The first checks and prepares the Java program; the second runs it.
javac ScopeExample.java
java ScopeExampleThe program prints 3, 10, 1, 2, and 3, each on a separate line.
- Inside show, the local score shadows the field. So score means the local value, 3.
- The name this.score refers to the field, which holds 10.
- A for loop repeats instructions. The step variable is available in the loop header and body, but not after the loop ends.
- The local score is still available after the loop because its block is the whole show method body.
The line beginning with two slashes is a comment, so Java does not run it. Remove the slashes and Java will reject the program: step is out of scope at that point.
A common mistake
Shadowing a field is allowed, but you cannot declare a second local variable with the same name while the first local variable is still in scope. For example, adding another declaration of score inside the loop body would make the example fail its Java checks.
You can declare a new local variable named step after the loop, because the loop variable’s scope has ended. It would be a separate variable, not the old one.
Exercise
In the example, what are the first two values printed if you remove the local declaration of score?
Show answer
Both are 10. Without the local variable, score refers to the field, just as this.score does.
Exercise
Why can you print step inside the loop but not after it? How could you fix the commented-out print line?
Show answer
Step is in scope only within the loop header and body. Move the print line into the loop body to print step there.