Public in AP Computer Science A
In AP Computer Science A, public is a Java access modifier that allows a class, constructor, method, or variable to be accessed from classes outside the declaring class. In this course, classes and constructors are always public, while instance variables are kept private.
This definition is part of Fiveable's AP Computer Science A review, which also has guided practice questions.
What is public?
Public is one of the two access keywords AP CSA expects you to use. The other is private. Putting public in front of a class, constructor, or method means code in other classes is allowed to use it. Per EK 3.3.A.1, private restricts access to the declaring class, while public allows access from classes outside the declaring class.
Think of a class like a restaurant. The public parts are the menu, so anyone can order from it. The private parts are the kitchen, which customers never walk into. In AP CSA, the rules are consistent. Classes are always public (EK 3.3.A.2), and constructors are always public (EK 3.3.A.3), so other code can create objects. Methods that make up a class's behavior are usually public too. Instance variables are the kitchen, so they stay private.
Why public matters in AP® Computer Science A
Public lives in Unit 3: Class Creation, Topic 3.3 Anatomy of a Class. It directly supports learning objective AP Comp Sci A 3.3.A, which asks you to develop code that designates access and visibility constraints for classes, data, constructors, and methods. The big idea behind it is data encapsulation (EK 3.3.A.1), which means keeping a class's implementation details hidden from outside classes. public decides what the outside world can see, so it is half of how encapsulation works. Every class you write on the FRQ section depends on getting public vs. private right.
Keep studying AP® Computer Science A Unit 3
How public connects across the course
Private (Unit 3)
Public and private are two sides of the same decision. Public opens a member to every class, and private locks it to the declaring class. Good class design means the public parts act as the controlled doorway to the private data.
Instance Variables (Unit 3)
Instance variables are the main thing you should NOT mark public. You keep them private and write public methods (like getters) so outside code can reach the data only in ways you allow.
Attributes and Behaviors (Unit 3)
A handy rule of thumb is that attributes (the data) are private and behaviors (the methods) are public. The public behaviors are the class's interface, meaning the list of things other code is allowed to ask an object to do.
Object Composition (Unit 3)
When one class has another object as an instance variable, the outer class can only use the inner object through its public methods. Public is what lets objects built from separate classes work together without reaching into each other's private data.
Is public on the AP® Computer Science A exam?
MCQ: Expect questions that show several class declarations and ask which one "demonstrates proper encapsulation by restricting access to instance variables while providing controlled access through methods." The right answer has private instance variables and public methods or constructors. Other questions trace code across classes and ask which statement causes a compile error. That is usually an outside class trying to touch a private variable directly when it should use a public method.
FRQ: public is everywhere in released free-response questions. In the 2017 exam, Q1 opens with public class Digits holding a private ArrayList, and Q3's Phrase class keeps its phrase in an instance variable that public methods access and change. Q2 even uses a public interface StudyPractice. When an FRQ asks you to write a full class, you are expected to declare the class and constructor public, make instance variables private, and give the required methods public headers that exactly match the specification. Making instance variables public can cost you points.
Public vs Private
Both are access modifiers, but they do opposite jobs. public means any class can access the member. private means only code inside the same class can access it. In AP CSA, classes and constructors are public and instance variables are private. A common mistake is thinking private hides data from the class's own methods. It does not. Private only blocks other classes, so your own methods can always read and change private instance variables.
Key things to remember about public
The public keyword allows a class, constructor, or method to be accessed from classes outside the declaring class.
In AP Computer Science A, classes are always declared public, and constructors are always public.
Instance variables should be private, with public methods providing controlled access to that data.
Public and private together make data encapsulation possible, which keeps a class's implementation details hidden from other classes.
On FRQs that ask you to write a class, use public for the class, constructor, and required methods, and private for instance variables.
If outside code tries to access a private member directly instead of going through a public method, the code will not compile.
Frequently asked questions about public
What does public mean in Java for AP CSA?
Public is an access modifier that lets code in any other class use a class, constructor, or method. It is tied to learning objective AP Comp Sci A 3.3.A in Topic 3.3 Anatomy of a Class.
Should instance variables be public in AP Computer Science A?
No. Instance variables should be private so the data stays encapsulated, and public methods like getters provide controlled access. Making instance variables public on a class-writing FRQ breaks encapsulation and can lose points.
What is the difference between public and private in Java?
Public allows access from classes outside the declaring class, and private restricts access to the declaring class only (EK 3.3.A.1). A class's own methods can still use its private variables. Only outside classes are blocked.
Do constructors have to be public on the AP exam?
Yes. In this course, constructors are always designated public (EK 3.3.A.3), so other classes can create objects using new.
Is public used on AP CSA free-response questions?
Constantly. Released 2017 FRQs open with headers like public class Digits and public interface StudyPractice, and you are expected to write public class, constructor, and method headers that match the given specifications.
Keep studying AP Computer Science A
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
AP Computer Science A hub
Review units, study guides, and course resources.
AP-style practice
Check this vocabulary in multiple-choice context.
FRQ practice
Apply key concepts in written AP responses.
Score calculator
Estimate the exam score you are working toward.
Cheatsheets
Review the highest-yield facts before practice.
Practice exam
Put the full course together before test day.