01 / 21

Scope and
Access

Understand how variables are visible across different parts of a Java program, and how access modifiers control what code can use them.

AP Computer Science A

What Is Scope?

Scope is the region of a program where a declared variable or method can be referenced. If you try to use a name outside its scope, the compiler produces an error: the name simply does not exist in that context.

Three Levels of Scope

Class-Level
Instance variables and static variables declared inside a class but outside any method. Visible to every method in the class.
Method-Level
Parameters and local variables declared inside a method. Visible only within that method, from declaration to closing brace.
Block-Level
Variables declared inside a loop, if-statement, or other { } block. Disappear the moment the block ends.

Scope Visualized

Class Scope private int score; Method Scope public void calculate(int x) { int result = x * 2; Block Scope for (int i = 0; i < 10; i++) { int temp = i + result; } } ← visible everywhere in class ← visible in this method only ← visible in this block only

Why Scope Matters

1
Prevents naming collisions: Two methods can each declare a local variable called temp without conflict.
2
Protects data integrity: Limiting visibility means fewer places in the code can accidentally modify a variable.
3
Enables encapsulation: Combined with access modifiers, scope is the foundation of information hiding in object-oriented design.

Access Modifiers in Java

Access modifiers are keywords placed before a class member declaration to control who can use that member. On the AP exam, you need to know two:

public
Accessible from any class in the program. Used for methods that other objects need to call.
private
Accessible only within the class that declares it. Used to hide instance variables and helper methods.

public vs private in Action

Student - name : String - gpa : double + getName() : String + getGpa() : double + setGpa(double) : void - = private + = public Main Student s = new Student(); s.getName() ✓ s.getGpa() ✓ s.name ✕ uses

Local vs Instance Variables

Local Variable Instance Variable
Declared in A method or block A class (outside methods)
Scope Method / block only Entire class
Default value None: must initialize 0, false, or null
Access modifier Not allowed private (recommended)
Lifetime Until method returns Until object is garbage-collected

The this Keyword

When a local variable has the same name as an instance variable, the local variable shadows (hides) the instance variable. Use this to refer to the instance variable explicitly.

public class Dog { private String name; public void setName(String name) { // 'name' alone refers to the parameter this.name = name; // assigns parameter to instance var } }

How to Apply Scope Rules

1
Identify where the variable is declared: Is it inside a class, method, or block?
2
Draw a mental brace map: The variable is visible from its declaration down to the matching closing brace }.
3
Check for shadowing: If a local variable shares the same name as an instance variable, the local takes priority. Use this to disambiguate.
4
Check access modifiers: If the variable is in a different class, only public members (or public getters/setters) are reachable.
5
Verify initialization: Local variables must be explicitly assigned a value before use.

Example 1: Variable Visibility

public class Counter { private int count = 0; public void increment() { int step = 1; count += step; } public void reset() { count = 0; // step is NOT visible here — compile error! } }

Example 2: Block Scope in Loops

public void printEvens(int n) { for (int i = 0; i < n; i++) { if (i % 2 == 0) { String msg = i + " is even"; System.out.println(msg); } // msg is NOT visible here } // i is NOT visible here }

Example 3: Shadowing Pitfall

public class Rect { private int width; public Rect(int width) { width = width; // BUG! assigns param to itself } }

After new Rect(5), the instance variable width is still 0 (the default for int). The parameter shadows the instance variable.

// Fix: use this.width public Rect(int width) { this.width = width; // ✓ Correct }

The Encapsulation Pattern

The AP CS A exam expects you to follow this pattern for every class:

1
Declare all instance variables as private.
2
Provide public getter methods to read values.
3
Provide public setter methods (when needed) to modify values, with optional validation logic.
4
Keep helper methods private if they are only used internally by the class.

Common Mistakes

✕ Using a Variable Outside Its Scope
Declaring a variable inside a loop and trying to use it after the loop ends. The variable no longer exists.
✕ Forgetting this Keyword
When a parameter name matches an instance variable, forgetting this. means the instance variable is never assigned.
✕ Making Instance Variables Public
Declaring public int score; violates encapsulation. The AP exam expects private fields with public accessors.

Scope and Access Summary

Class-Level Method-Level Block-Level
Keyword private / public None None
Visibility Whole class One method One { } block
Default value Yes (0 / null) No No
Typical use Object state Temporary calculations Loop counters
Lifetime Object lifetime Method call Block execution

Question 1 of 5

What happens when you try to compile this class?

Question 2 of 5

Which access modifier should be used for instance variables according to AP CS A best practices?

Question 3 of 5

What does new Car("red").getColor() return?

Question 4 of 5

What is the result of calling mystery()?

Question 5 of 5

What happens when the Bank class is compiled?