Access Modifiers

Access modifiers are Java keywords (public and private on the AP CSA exam) that control which parts of a program can see and use a class, method, or instance variable. Public means anyone can access it; private means only code inside the same class can.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What are Access Modifiers?

Access modifiers are the keywords you put in front of classes, methods, constructors, and instance variables to decide who gets to touch them. public means any code anywhere can use it. private means only code written inside that same class can use it. Java also has protected and default (package) access, but the AP Java subset only tests public and private, so those two are all you need.

The standard AP pattern looks like this: the class is public, instance variables are private, and the constructors and methods that outside code needs are public. That setup is called encapsulation. The data is locked inside the class, and the only way in is through the public methods the class chooses to offer. Think of a class like a vending machine. The buttons (public methods) are how you interact with it, but the inventory inside (private instance variables) is off-limits unless the machine hands it to you.

Why Access Modifiers matter in AP Computer Science A

Access modifiers live at the heart of class design in AP CSA, the material on writing your own classes with instance variables, constructors, and methods. The CED expects you to designate instance variables as private and provide public methods to interact with them, because data protection through encapsulation is one of the big ideas behind object-oriented design.

This matters directly for your score. The Class FRQ (one of the four free-response questions every year) asks you to write an entire class from scratch, and graders check whether your instance variables are private. Writing public int grade; when the design calls for private access costs you points even if the rest of your logic works. Access modifiers are also a favorite MCQ trap, especially questions about where modifiers are and aren't allowed.

How Access Modifiers connect across the course

Private Access (Unit 3)

Private is the modifier you'll write most on the exam. Instance variables should be private so outside code can't change an object's state directly. If another class needs that data, it has to go through a public getter method instead.

Public Access (Unit 3)

Public is the interface of your class, the constructors and methods outside code is allowed to call. The exam pattern is simple to memorize. Class public, instance variables private, methods and constructors public unless the question says otherwise.

Local Variable Scope (Units 1-3)

Local variables declared inside a method or constructor never get an access modifier. They already only exist inside that block, so writing private String tempName = n.trim(); inside a constructor is a compile error. This is a classic MCQ trick.

Data Types (Unit 1)

A full variable declaration in a class reads modifier, then type, then name, like private int grade;. The modifier controls who can access the variable, while the data type controls what kind of value it holds. Two separate jobs, and the exam tests both.

Are Access Modifiers on the AP Computer Science A exam?

Multiple-choice questions love testing where access modifiers can legally appear. A classic stem asks whether local variables can be declared public or private (they can't, and that code won't compile). Another common setup shows a constructor with something like private String tempName = n.trim(); inside it and asks you to spot the error. The fix is to drop the modifier, since local variables don't take one.

On the free-response section, access modifiers show up every single year in the Class FRQ. When you write a class, you're expected to make instance variables private and the constructor and methods public. The scoring guidelines specifically reward appropriate use of private for instance data, so this is one of the easiest points to lock in if you've drilled the pattern.

Access Modifiers vs Variable scope

Access modifiers and scope both limit where a variable can be used, but they're different mechanisms. Scope is automatic and based on where a variable is declared (a local variable only exists inside its method or block). Access modifiers are keywords you choose, and they only apply to class-level things like instance variables, methods, and constructors. That's exactly why local variables can't be public or private. Scope already handles them, so Java forbids the modifier entirely.

Key things to remember about Access Modifiers

  • Access modifiers are keywords that control which code can use a class, method, constructor, or instance variable.

  • The AP CSA exam only tests public and private; protected and default access are outside the AP Java subset.

  • The standard exam pattern is a public class, private instance variables, and public constructors and methods. This is encapsulation.

  • Local variables inside methods and constructors cannot have an access modifier; adding one is a compile-time error the exam loves to test.

  • On the Class FRQ, declaring instance variables as private is worth points, so make it a habit every time you write a class.

Frequently asked questions about Access Modifiers

What are access modifiers in AP Computer Science A?

Access modifiers are Java keywords that control visibility. Public means any code can access the class, method, or variable; private means only code inside the same class can. They're how Java enforces encapsulation in the classes you write on the exam.

Can local variables be public or private in Java?

No. Local variables declared inside a method or constructor cannot have any access modifier, and code like private String tempName = n.trim(); inside a constructor won't compile. Their visibility is already limited by scope to the block they're declared in.

Is protected access on the AP CSA exam?

No. The AP Java subset only includes public and private. You'll never need protected or default (package) access on the exam, even though full Java has all four levels.

What's the difference between an access modifier and a data type?

In a declaration like private int grade;, the access modifier (private) says who can use the variable, and the data type (int) says what kind of value it stores. The modifier is about visibility; the type is about the data itself.

Why should instance variables be private on the AP exam?

Private instance variables enforce encapsulation, meaning outside code can only change an object's data through the public methods the class provides. The Class FRQ scoring guidelines award credit for this, so making instance variables private is one of the most reliable points on the free-response section.