Public Access Modifier

In AP Computer Science A, the public access modifier (the keyword public in Java) makes a class, method, or variable accessible from any other class. AP convention is public classes and methods with private instance variables, so outside code uses your methods but can't touch your data directly.

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

What is Public Access Modifier?

The public access modifier is the Java keyword public placed before a class, method, constructor, or variable declaration. It means "anyone can use this." Any code in any other class can call a public method or read a public variable. Think of it as the front door of a class. Public methods are the doorbell and mail slot, the official ways the outside world interacts with your object.

In AP CSA, public works as one half of a design pattern you'll use constantly. Classes, constructors, and methods are usually public so other code can create objects and call their behaviors. Instance variables are usually private so no outside code can reach in and change an object's data directly. This pairing is the heart of encapsulation (also called data hiding or access control). You decide what the world gets to see (the public interface) and what stays internal (the private data). Even main follows this rule. Its full signature is public static void main(String[] args), and the public part is what lets the Java runtime find and call it from outside your class.

Why Public Access Modifier matters in AP Computer Science A

Access modifiers show up the moment AP CSA starts teaching you to write classes instead of just use them. The course expects you to designate access for classes, data, constructors, and methods, and the standard it teaches is simple. Make the class and its methods public, make the instance variables private. This isn't just style. It's how Java enforces encapsulation, one of the big ideas of object-oriented design that runs through the whole course, from writing your first class to building inheritance hierarchies. If you mark an instance variable public on a class-design FRQ, you're technically letting any code corrupt your object's state, and graders expect to see private there. Understanding what public actually permits (and what private forbids) is also a classic multiple-choice trap, since questions love to show code that tries to access something it can't and ask whether it compiles.

How Public Access Modifier connects across the course

Private Access Modifier

Public and private are two settings on the same dial. Public means any class can access it, private means only code inside the same class can. AP CSA's rule of thumb pairs them. Public methods form the interface, private variables hold the data behind it.

Class

Every class you write for AP CSA is declared public class ClassName. That public declaration is what lets other classes (like a tester or a driver class) create objects of your class and call its methods at all.

Main Method

The signature public static void main(String[] args) only works because of the public part. The Java runtime lives outside your class, so main has to be publicly visible for the program to even start. It's the most famous public method you'll ever write.

Access Control

Public is one tool in the broader idea of access control, which is the practice of deciding who gets to see what. Choosing public versus private for each member is how you implement encapsulation, and that decision-making is exactly what class-design FRQs grade you on.

Is Public Access Modifier on the AP Computer Science A exam?

On the free-response section, the class-design question asks you to write a complete class, and the expected pattern is a public class with public methods and constructors and private instance variables. You won't lose points for writing public on a method, but writing public on an instance variable goes against the encapsulation conventions the course teaches, so default to private data. On multiple choice, access modifiers show up in "does this compile?" questions. A common stem shows one class trying to directly access another class's variable, and you have to know that direct access only works if that member is public. Also remember that within the same class, public versus private makes no difference, since a class can always access its own members.

Public Access Modifier vs Private Access Modifier

Public means accessible from anywhere, private means accessible only inside the class where it's declared. Students mix up when to use each. The AP convention is public for the things outside code needs (the class itself, constructors, methods) and private for instance variables, which outside code should only reach through public getter and setter methods. If you remember "public behavior, private data," you've got the pattern the FRQ rubric expects.

Key things to remember about Public Access Modifier

  • The public keyword makes a class, method, or variable accessible from any other class in the program.

  • AP CSA convention is public classes, constructors, and methods paired with private instance variables, and the class-design FRQ expects this pattern.

  • Public methods form a class's interface, meaning they are the official way outside code interacts with an object's private data.

  • The main method must be public because the Java runtime calls it from outside your class.

  • Access modifiers only restrict access from outside a class; code inside a class can always use its own members regardless of modifier.

Frequently asked questions about Public Access Modifier

What is the public access modifier in Java?

It's the keyword public placed before a class, method, constructor, or variable, and it makes that item accessible from any other class. In AP CSA you'll use it on nearly every class and method you write.

Should instance variables be public or private on the AP exam?

Private. AP CSA teaches encapsulation, so the class-design FRQ expects private instance variables accessed through public methods. Making instance variables public goes against the conventions the course grades you on.

Does public mean anyone can change a variable?

Yes, if the variable itself is public, any class can read and modify it directly. That's exactly why AP CSA tells you to make instance variables private and control changes through public methods instead.

What's the difference between public and private in Java?

Public members are accessible from any class; private members are accessible only inside the class that declares them. The AP pattern is public for methods and constructors, private for instance variables.

Why is the main method public in Java?

Because the Java Virtual Machine, which lives outside your class, has to be able to find and call main to start the program. That's why the required signature is public static void main(String[] args).