From the free course AP Computer Science A on Lykke
3.8 Scope and Access
Key concepts: Scope and access of variables · Variables declared in block headers or bodies · Local variables · Block-level visibility · Accessing variables only within their declaring block
A variable is usable only inside the region of code where its declaration is visible. That region is its scope: the part of a program in which a variable can be referred to by name.
3.8 Scope and Access
A variable is usable only inside the region of code where its declaration is visible. That region is its scope: the part of a program in which a variable can be referred to by name.
Imagine each pair of braces as a room. A variable declared inside a room can be used there, but code outside the room cannot see it. A variable can be declared in either the header or the body of a block of code, and the location of that declaration determines where the variable may be used.
Blocks create visibility boundaries
A block is a sequence of statements enclosed by braces, such as the body of a method, constructor, loop, or conditional statement. A variable declared inside a block is a local variable. Local variables can only be accessed within the block in which they are declared.
The boundary is lexical: Java determines visibility from where the declaration appears in the source code, not from which statement happens to run first.
public void displayTicket(int seatNumber) {
int price = 12;
if (seatNumber < 10) {
int discount = 3;
System.out.println(price - discount); // valid
}
System.out.println(price); // valid
// System.out.println(discount); // compilation error
}
Here, price is declared in the method block, so it can be used throughout that method. discount is declared in the if block, so its scope ends at the closing brace of that if statement.
Variables in block headers
A declaration in a block header follows the same rule. In a for loop, the loop-control variable is declared in the header and is available inside the loop block, but not after the loop.
int total = 0;
for (int count = 1; count <= 3; count++) {
total += count;
System.out.println(count); // valid
}
// System.out.println(count); // compilation error
The variable count belongs to the for statement’s scope. Once execution leaves the loop, the name count is no longer accessible. The variable total, however, was declared in the surrounding block and remains accessible after the loop.
Method parameters are local variables
A constructor or method is itself a block of code. Therefore, each parameter in its header is a local variable. A parameter may be used within that constructor or method, but not by unrelated methods.
public class Lamp {
private int brightness;
public Lamp(int startingBrightness) {
brightness = startingBrightness;
}
public void setBrightness(int newBrightness) {
brightness = newBrightness;
}
public void showBrightness() {
// System.out.println(newBrightness); // compilation error
System.out.println(brightness);
}
}
startingBrightness is local to the constructor, and newBrightness is local to setBrightness. The instance variable brightness has a different scope because it belongs to each Lamp object and can be accessed by the appropriate instance methods. The parameter names do not persist after their method finishes.
Worked example: locating the legal uses
Consider this method:
public int calculateFare(int miles) {
int baseFare = 4;
if (miles > 10) {
int extraFare = miles - 10;
baseFare += extraFare;
}
return baseFare;
}
Trace the declarations from the outside inward:
| Variable | Declared in | Legal access |
|---|---|---|
miles |
Method header | Anywhere in calculateFare |
baseFare |
Method body | Anywhere in calculateFare after its declaration |
extraFare |
if block body |
Only inside that if block |
The final return baseFare; compiles because baseFare belongs to the method block. A statement such as return extraFare; would not compile because extraFare belongs only to the nested if block.
Common misconception: “If the code ran there, the variable still exists everywhere”
Misconception: A variable declared inside a conditional or loop can be used later because the conditional or loop has already executed.
Correction: Execution does not expand scope. Scope is determined by the declaration’s location. Even if the if condition was true, a variable declared inside its block remains inaccessible after the closing brace.
Misconception: A method parameter is available to every method in the class.
Correction: A parameter is a local variable. It belongs only to the constructor or method whose header declares it.
AP skill connections
This topic is assessed through 3.B: Determine the result or output based on code that contains data abstractions. You must identify which declarations are visible at each statement before tracing values.
It also develops 3.D: Explain why a code segment will not compile or work as intended and modify the code to correct the error. An out-of-scope variable is a compile-time error; a correction usually moves the declaration outward or passes the needed value as a parameter.
Finally, 4.A: Describe the behavior of a code segment or program requires explaining not only what executes, but also which names are legally accessible during execution.
Retrieval check
A variable x is declared inside the body of a while loop. Can a statement after the loop use x? No. Unless x was declared in an enclosing block, its scope ends at the loop’s closing brace. A variable’s declaration location—not the order in which statements execute—controls access.

Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A