What is the scope of a local variable, and how do public and private control access to members?
Topic 5.8 Scope and Access: distinguish local variable scope from instance variable scope, and use the access modifiers public and private to control where a class member can be used.
A focused answer to AP CSA Topic 5.8, covering the scope of local variables and parameters versus instance variables, variable shadowing, the public and private access modifiers, and how access control supports encapsulation, with a fully worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 5.8) wants you to understand scope - where a variable can be used - and access, controlled by the modifiers public and private. Local variables and parameters are visible only inside the block or method where they are declared, while instance variables are visible throughout the object. Access modifiers determine which parts of a program may use a member, and they are the mechanism behind encapsulation.
Local variable scope
public void m() {
int a = 1; // in scope for the whole method
for (int i = 0; i < 3; i++) {
int b = i * 2; // b and i are in scope only inside the loop
}
// here a is in scope; b and i are NOT
}
A for loop's control variable, declared in its header, is also limited to the loop.
Instance variable scope
An instance variable, declared in the class body, is in scope in every method of the object. That is what lets accessors and mutators in different methods all read and write the same private field.
Shadowing
This is why setter parameters are sometimes named differently from the field, or qualified with this, to avoid accidentally assigning a parameter to itself.
Access modifiers: public and private
A private instance variable is invisible to code in other classes; they must go through public accessors and mutators. This lets the class validate changes and hide its internal representation.
Try this
Q1. A variable is declared inside a for loop body. Where is it in scope? [1 point]
- Cue. Only inside that loop body (the braces); it does not exist before or after the loop.
Q2. Explain the difference between public and private access, and why instance variables are usually private. [2 points]
- Cue.
publicmembers are accessible from any class;privatemembers only within their own class. Instance variables areprivateso outside code cannot change them directly, letting the class control and validate its data (encapsulation).
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. Consider the following method.
```java
public void demo() {
int x = 5;
if (x > 0) {
int y = 10;
x = x + y;
}
System.out.println(x + y);
}
```
What happens?
(A) It does not compile, because y is out of scope at the println.
(B) It prints `15`.
(C) It prints `25`.
(D) It prints `5`.
(E) It prints `10`.
Show worked answer →
The answer is (A).
y is declared inside the if block, so its scope is limited to that block - it does not exist after the closing brace. The println is outside the if, so y is out of scope there, and the reference x + y fails to compile. (B) and (C) assume y is still visible. The fix would be to declare y before the if if it is needed afterward.
Markers reward knowing that a variable declared inside a block is only in scope within that block, including the braces of an if.
AP 2020 (style)3 marksFree response (code writing). A class `Locker` has a private int instance variable `code`. Write a public accessor `getCode` that returns `code`, and explain in a one-line comment why making `code` private rather than public improves the design.
Show worked answer →
A 3-point question testing access control and encapsulation.
// code is private so it can only be read or changed through this class's
// methods, which lets the class control and protect its data (encapsulation).
public int getCode() {
return code;
}
Point 1: a public accessor with the correct return type. Point 2: it returns the private instance variable. Point 3: the comment explains that private restricts direct access to the class's own methods, supporting encapsulation. Stating that private only affects speed, or that it makes the variable shared, would be wrong.
Related dot points
- Topic 5.1 Anatomy of a Class: identify the parts of a class definition (the class header, private instance variables, constructors and methods) and explain encapsulation through access modifiers.
A focused answer to AP CSA Topic 5.1, covering the class header, private instance variables, constructors and methods, the meaning of public and private, encapsulation, and how the pieces combine into a working class, with a fully worked example.
- Topic 5.7 Static Variables and Methods: declare and use static variables and methods, which belong to the class as a whole rather than to any one object, and explain how they differ from instance members.
A focused answer to AP CSA Topic 5.7, covering the static keyword, class-level variables shared by all objects, static methods called on the class, why static methods cannot use instance variables, and counting objects with a static field, with a fully worked example.
- Topic 5.6 Writing Methods: design and implement methods that take parameters, perform a computation possibly using the object's instance variables, and return a result or perform an action.
A focused answer to AP CSA Topic 5.6, covering the method header (modifiers, return type, name, parameters), how a method uses instance variables and parameters, returning a value versus void, calling one method from another, and tracing a method, with a fully worked example.
- Topic 5.9 this Keyword: use the implicit parameter this to refer to the current object, disambiguate an instance variable from a parameter of the same name, and pass the current object to another method.
A focused answer to AP CSA Topic 5.9, covering what this refers to, using this to distinguish an instance variable from a shadowing parameter, calling another method on the current object with this, and passing this as an argument, with a fully worked example.
- Topic 5.5 Mutator Methods: write mutator (setter) methods, usually void, that change an object's instance variables, including methods that validate or constrain the new value before assigning it.
A focused answer to AP CSA Topic 5.5, covering mutator (setter) methods, the void return type, changing instance variables from a parameter, validating a new value before assigning, and why mutators protect encapsulated data, with a fully worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)