---
title: "Access Modifiers — AP Comp Sci A Definition & Exam Guide"
description: "Access modifiers (public, private) control who can use a class's variables and methods in Java. On AP CSA, they're the backbone of encapsulation and class design FRQs."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/access-modifiers"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 1"
---

# Access Modifiers — AP Comp Sci A Definition & Exam Guide

## Definition

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.

## What It Is

Access modifiers are the keywords you put in front of classes, [methods](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink"), constructors, and [instance variables](/ap-comp-sci-a/key-terms/instance-variables "fv-autolink") 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](/ap-comp-sci-a/unit-1/why-programming-why-java/study-guide/lVK6rmrBuug17i1Hna9z "fv-autolink") 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 It Matters

Access modifiers live at the heart of class design in AP CSA, the material on writing your own classes with instance variables, [constructors](/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk "fv-autolink"), and methods. The CED expects you to designate instance variables as [private](/ap-comp-sci-a/key-terms/private "fv-autolink") 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.

## Connections

### 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](/ap-comp-sci-a/key-terms/objects-state "fv-autolink") directly. If another class needs that data, it has to go through a public getter method instead.

### Public Access (Unit 3)

The exam pattern is simple to memorize. Class [public](/ap-comp-sci-a/key-terms/public "fv-autolink"), 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](/ap-comp-sci-a/key-terms/access-modifier "fv-autolink"). 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.

## On the AP 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 Takeaways

- 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.

## FAQs

### 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.

## Related Study Guides

- [1.2 Variables and Primitive Data Types](/ap-comp-sci-a/unit-1/variables-and-primitive-data-types/study-guide/rezA6f3hJz84TKaY5Jjl)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/access-modifiers#resource","name":"Access Modifiers — AP Comp Sci A Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/access-modifiers","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/access-modifiers#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/access-modifiers#term","name":"Access Modifiers","description":"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.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/access-modifiers","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What are access modifiers in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can local variables be public or private in Java?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Is protected access on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What's the difference between an access modifier and a data type?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Why should instance variables be private on the AP exam?","acceptedAnswer":{"@type":"Answer","text":"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."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 1","item":"https://fiveable.me/ap-comp-sci-a/unit-1"},{"@type":"ListItem","position":4,"name":"Access Modifiers"}]}]}
```
