5m left·0%
Reading Time: 5 min
Last Updated: March 9, 2026
Main Ideas: 5
Reading Time: 5 min
Last Updated: March 9, 2026
Main Ideas: 5

Topic 3.8 Notes – Scope and Access

Verified for 2027 AP® Computer Science A Exam
Read aloud
Scope answers one question: where can this variable be used? In Java, every variable lives inside a specific block of code. If you know where it was declared, you know where it works.

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 if block

A variable only exists inside the block where it’s declared.

Here’s a visual of nested blocks in a simple Java program:

Study guide illustration

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 public or private
  • 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 public or private
  • 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 this when names match

If something “mysteriously” doesn’t change in a method, check for shadowing.

Key Takeaways

A variable can only be used inside the block where it is declared.
Parameters are local variables and cannot be public or private.
Loop variables disappear after the loop ends.
If a parameter and instance variable share a name, the parameter wins inside the method.
Use this.variableName to refer to the instance variable during shadowing.
Static methods cannot directly access instance variables.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining