Topic 3.8 Notes – Scope and Access
What Scope Is
Scope = the region of code where a variable is accessible.
In AP CSA, variables live inside blocks like:
- A class
- A constructor
- A method
- A loop or
ifblock
A variable only exists inside the block where it’s declared.
Here’s a visual of nested blocks in a simple Java program:

Nested scopes in a Java class
Notice how the class contains the method, and the method contains the loop. Each pair of braces creates a new, smaller scope.
The deeper the box, the more limited the scope.
Types of Variables and Where They Can Be Used
There are three types you need to clearly separate in your head:
- Instance variables
- Local variables
- Parameters
Instance Variables
Declared in the class, outside any method or constructor.
public class Player {
private int score; // instance variable
public void addPoints(int pts) {
score += pts; // allowed
}
}
Key facts:
- Belong to each object
- Accessible anywhere inside the class
- Usually
private - Live as long as the object exists
On FRQs, these are almost always private and accessed through methods.
Local Variables
Declared inside a method, constructor, or block.
public void calculate() {
int total = 0; // local variable
}
Important rules:
- Only usable inside that method/block
- Cannot be
publicorprivate - Disappear when the method finishes
Block scope example:
if (true) {
int x = 10;
}
// x cannot be used here
Classic quiz mistake: using a loop variable after the loop.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// i does NOT exist here
Parameters
Parameters are local variables declared in a method header.
public void setLevel(int level) {
// level is a local variable
}
They:
- Exist only inside that method
- Cannot be
publicorprivate - Disappear after the method finishes
Parameters behave exactly like other local variables.
Shadowing
Shadowing happens when a parameter or local variable has the same name as an instance variable.
private int score;
public void setScore(int score) {
score = score; // problem
}
Inside this method, score refers to the parameter.
The instance variable is hidden.
So this does nothing.
Using this to Fix It
this.variableName refers to the instance variable.
public void setScore(int score) {
this.score = score;
}
Left side → instance variable
Right side → parameter
If you ever see this. on a quiz, it’s almost always resolving shadowing.
Static vs Instance Context
This connects to scope because context determines what you’re allowed to access.
- Instance methods can access:
- Instance variables
- Static variables
- Static methods can access:
- Static variables only (directly)
Example:
private int count;
private static int total;
public static void printData() {
System.out.println(total); // OK
System.out.println(count); // ERROR
}
Why? A static method doesn’t belong to an object.
It has no specific count to refer to.
This shows up constantly with public static void main.
Inside main, you cannot directly access instance variables without creating an object.
How This Gets Tested
Multiple choice often gives code and asks:
- Which line causes a compile error?
- Which variable is accessible at this point?
- What prints after shadowing?
FRQs expect:
- Instance variables declared
private - Parameters used correctly
- Proper use of
thiswhen names match
If something “mysteriously” doesn’t change in a method, check for shadowing.