Access modifiers: who is allowed to see what.
A single keyword controls whether other classes can use a field or method — and on the AP exam, that keyword is almost always either public or private.
An access modifier is a Java keyword placed before a field, method, or class declaration that controls its visibility — which other code can see and use it.
AP CSA focuses on two modifiers: public and private. They are the foundation of encapsulation. Knowing which to apply to each member of your class is a graded skill on FRQ #2.
Access modifiers appear in MCQs about whether client code compiles, in FRQ class design, and in conceptual questions about good design.
Public is open; private is locked.
Choose the most restrictive modifier that still gets the job done.
public
public class Account { ... }
public String getName() { ... }
Anyone can use a public class or member. Use it for things that are part of the class's public interface — the methods clients are meant to call.
private
private int balance;
private boolean isValidAmount(double amt) { ... }
Only code inside the same class can access a private member. Use it for instance variables and helper methods that shouldn't be exposed.
Where each belongs
- Instance variables →
private. - Accessor / mutator / behavior methods →
public(because clients need to call them). - Helper methods →
private. - Constructors → typically
public. - The class itself → typically
public.
Visibility from inside vs outside the class
public class Account {
private double balance;
public double getBalance() {
return balance; // OK: inside the class
}
}
// Client code (outside the class):
Account a = new Account();
double b = a.balance; // compile error: balance is private
double b2 = a.getBalance(); // OK: getBalance is public
The other modifiers (background only)
Java also has protected and default (package) access. These are largely outside the AP CSA tested scope; you may see them in MCQs occasionally, but FRQs only require public and private.
Visibility terminology.
Used in MCQ stems about compilation and access.
- Access modifier — a keyword that controls visibility (
public,private). - Visibility — which code can refer to a member by name.
- Public — accessible from any class.
- Private — accessible only from within the declaring class.
- Client code — code that uses a class from outside it.
- Public interface — the
publicmethods and constructors of a class. - Implementation — the private fields and helpers hidden from clients.
How access modifiers are tested.
"Does this line compile?" is the most common MCQ form.
MCQ patterns to expect:
- Direct field access from outside the class. If the field is
private, the line won't compile. - Calling a private helper from another class. Compile error.
- Choosing the best design. Look for private fields plus public methods.
FRQ rubric items:
- All instance variables declared
private. - All methods declared
public, except internal helpers. - No accidental package-private declarations (e.g., forgetting
publicorprivateon a member).
Decide whether each client line compiles.
Apply the visibility rules to each access in turn.
Problem. Given the class below, which client lines compile?
public class Robot {
private int x;
public int speed;
public Robot(int startX) { x = startX; }
public int getX() { return x; }
private void log(String s) { System.out.println(s); }
}
// Client code in a different class:
Robot r = new Robot(5);
int a = r.x; // (1)
int b = r.speed; // (2)
int c = r.getX(); // (3)
r.log("hello"); // (4)
Line (1): x is private. Outside the class, this is a compile error.
Line (2): speed is public. This compiles — though making it public is poor design.
Line (3): getX is public. Compiles and returns 5.
Line (4): log is private. Compile error from outside the class.
Answer. Only lines (2) and (3) compile.
Why this matters. AP MCQs frequently include four lines and ask "which compile?" Walking through each access individually is the fastest, safest method.
Modifier mistakes that derail your class.
Most come from leaving the modifier off entirely.
- Omitting the modifier. Without one, the member is package-private — a subtle bug; FRQ rubrics expect explicit
publicorprivate. - Making instance variables
public. Breaks encapsulation and loses FRQ points. - Making methods
privatewhen clients need to call them. - Accessing a
privatefield from another class. Compile error. - Mixing class-level and member-level modifiers. A
privateclass isn't allowed at the top level in AP code. - Using
protectedin FRQ answers. Stick topublicandprivateunless the prompt uses it first.
Access modifiers at a glance.
Pick the right modifier for each part of your class.
Access Modifiers Reference
AP Quick Reference| Member | Modifier | AP Exam Tip |
|---|---|---|
| Class | public |
Standard for AP CSA code. |
| Instance variable | private |
Public fields are penalized on FRQs. |
| Constructor | public |
Clients must be able to call new. |
| Accessor / mutator | public |
The public interface to the data. |
| Behavior method | public |
Clients need to invoke it. |
| Helper method | private |
Hides internal logic. |
| Constant | public static final |
Safe to expose; cannot be changed. |
How to make modifier choice automatic.
A two-line rule covers every AP CSA case.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each line of client code, name the member's modifier and decide if the line compiles. Build this into reflex.
- Java Lab — declare every member with an explicit modifier. Test that private members cannot be reached from a separate test class.
- FRQ Practice — before submitting, scan your class: "All fields private? All methods public? Helpers private?" If yes, you've earned the encapsulation marks.
The two-line rule: fields private, methods public unless they're helpers. That single line handles nearly every AP CSA class.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.