TLDR
The anatomy of a Java class is the structure that holds your object's data and behaviors together: a class header, private instance variables, public constructors, and methods. In AP Computer Science A, you control who can see and use each part with the keywords public and private, which is how you achieve data encapsulation. Keep instance variables private and expose behavior through public methods so outside classes interact with your object in safe, predictable ways.

Why This Matters for the AP Computer Science A Exam
Designing and reading classes shows up on both the multiple-choice and free-response sections. You need to look at a class definition and decide whether it is set up correctly: is the class header right, are instance variables private, are constructors and methods declared with the correct access? On free-response code writing, you will design and implement a class from a specification, and that means writing a public class header, private instance variables, public constructors, and methods with the right visibility. Getting the anatomy right is the base layer for every later Unit 3 topic, including constructors, writing methods, scope, and the this keyword.
Key Takeaways
- A class is declared with the keyword
class, and in this course classes are alwayspublic. - Instance variables belong to each object, so every object gets its own copy of those variables.
- Mark instance variables
privateunless the specification says otherwise; this is how you keep an object's data encapsulated. - Constructors in this course are always
public. privaterestricts access to the declaring class only;publicallows access from outside classes.- Public methods can be called inside or outside the class, while private methods can only be called inside the class.
Key Concepts
The Class Declaration
Every class starts with a class declaration that sets up the blueprint for objects.
</>Javapublic class Student { // Everything else goes inside these braces }
In AP Computer Science A, classes are always declared public using the keyword class. The public keyword means other classes can create and use Student objects. Choose a class name that is a noun describing what objects of this type represent, and use Pascal case (capitalize the first letter of each word).
Instance Variables: The Object's Data
Instance variables define the data each object stores. These are the attributes that make each object's state unique. Instance variables belong to the object, and each object has its own copy.
</>Javapublic class Student { // Instance variables - each Student object has its own copy private String name; private int gradeLevel; private double gpa; private boolean isHonorsStudent; // More code will go here... }
These are marked private on purpose. That is data encapsulation: the implementation details are kept hidden from outside classes, so other classes can only reach this data through methods you choose to provide. Good practice is to make instance variables private unless the class specification tells you to do otherwise.
Each Student object created from this class has its own name, gradeLevel, gpa, and isHonorsStudent.
Methods: The Object's Behavior
Methods define what objects of this class can do. They are the behaviors of your class.
</>Javapublic class Student { private String name; private int gradeLevel; private double gpa; private boolean isHonorsStudent; // Constructor - sets the initial state of an object public Student(String studentName, int grade) { name = studentName; gradeLevel = grade; gpa = 0.0; // Default starting GPA isHonorsStudent = false; // Default to regular student } // Accessor method - provides controlled access to private data public String getName() { return name; } // Mutator method - provides controlled way to change private data public void updateGPA(double newGPA) { if (newGPA >= 0.0 && newGPA <= 4.0) { gpa = newGPA; isHonorsStudent = (gpa >= 3.5); // Update honors status } } // Returns a classification based on grade level public String getClassification() { if (gradeLevel == 9) return "Freshman"; else if (gradeLevel == 10) return "Sophomore"; else if (gradeLevel == 11) return "Junior"; else if (gradeLevel == 12) return "Senior"; else return "Unknown"; } }
Constructors set the initial state of objects, accessors provide safe access to private data, and mutators allow controlled changes to that data.
Access Modifiers: Controlling Visibility
Access modifiers control which parts of your class other classes can see and use. The two keywords you need here are public and private.
private: Access is restricted to the declaring class. Use this for internal data and helper methods.
</>Javaprivate String name; // Only Student class methods can access this private void calculateHonorsStatus() { // Helper method, internal use only isHonorsStudent = (gpa >= 3.5); }
public: Any other class can access it. Use this for the interface your class provides to the outside world.
</>Javapublic String getName() { // Other classes can call this method return name; }
Access to attributes should stay internal to the class to accomplish encapsulation, so keep instance variables private. Access to behaviors can be internal or external: public methods can be called inside or outside the class, while private methods can only be called inside the class.
The Complete Pattern
Here is how the pieces work together in a well-structured class:
</>Javapublic class BankAccount { // Private instance variables - the internal state private String accountNumber; private String ownerName; private double balance; private int transactionCount; // Public constructor - how objects are created public BankAccount(String number, String owner, double initialBalance) { accountNumber = number; ownerName = owner; balance = initialBalance; transactionCount = 0; } // Public accessor methods - controlled read access public String getAccountNumber() { return accountNumber; } public String getOwnerName() { return ownerName; } public double getBalance() { return balance; } // Public mutator methods - controlled write access public void deposit(double amount) { if (amount > 0) { balance += amount; transactionCount++; } } public boolean withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; transactionCount++; return true; // Successful withdrawal } return false; // Failed withdrawal } // Public method that builds a summary string public String getAccountSummary() { return ownerName + "'s account #" + accountNumber + " has balance $" + balance + " (" + transactionCount + " transactions)"; } // Private helper method - internal implementation detail private boolean validateTransaction(double amount) { return amount > 0 && amount <= 10000; // Example business rule } }
The internal details stay hidden, but the class offers a clean public interface for other classes to use. That is encapsulation in action.
Code Examples
Comparing Strong vs Weak Class Design
Weak design: everything public, no protection of data.
</>Java// No encapsulation - data is exposed and unvalidated public class WeakStudent { public String name; // Anyone can change this directly public int gradeLevel; // No validation possible public double gpa; // Could be set to invalid values }
Strong design: private data with controlled access.
</>Java// Well-organized and protected public class StrongStudent { // Private data - controlled access only private String name; private int gradeLevel; private double gpa; // Clear constructor public StrongStudent(String studentName, int grade) { name = studentName; setGradeLevel(grade); // Use validation gpa = 0.0; } // Controlled access with validation public void setGradeLevel(int grade) { if (grade >= 9 && grade <= 12) { gradeLevel = grade; } } public void updateGPA(double newGPA) { if (newGPA >= 0.0 && newGPA <= 4.0) { gpa = newGPA; } } public boolean isEligibleForHonorRoll() { return gpa >= 3.5; } }
Method Organization
Organizing your methods in a predictable order makes a class easier to read:
</>Javapublic class Rectangle { private double width; private double height; // 1. Constructor first public Rectangle(double w, double h) { setWidth(w); setHeight(h); } // 2. Accessor methods (getters) public double getWidth() { return width; } public double getHeight() { return height; } public double getArea() { return width * height; } public double getPerimeter() { return 2 * (width + height); } // 3. Mutator methods (setters) public void setWidth(double w) { if (w > 0) width = w; } public void setHeight(double h) { if (h > 0) height = h; } // 4. Other behavior methods public boolean isSquare() { return width == height; } public void scale(double factor) { if (factor > 0) { width *= factor; height *= factor; } } // 5. Private helper methods last private boolean isValidDimension(double dimension) { return dimension > 0; } }
A consistent layout makes your class easy to navigate.
How to Use This on the AP Computer Science A Exam
Free Response
When you design a class from a specification, build it in a reliable order:
- Write the
public classheader with the keywordclass. - Declare
privateinstance variables for each attribute. - Write
publicconstructors that set the initial state. - Write methods with the visibility the problem requires, usually
publicfor behaviors others need to call.
A specification often comes as a table showing how to interact with the class and what the results should be. Use that table to decide what instance variables you need and what methods must exist. Mark instance variables private unless the problem explicitly says otherwise.
Code Tracing
For multiple-choice and reading questions, check a class for correct visibility:
- Is the class declared
publicwith the keywordclass? - Are instance variables
private? - Are constructors
public? - Can a line of code in another class reach a
privatemember? If it tries to, that line will not compile.
Common Trap
If outside code tries to call a private method or read a private variable directly, the code does not compile. The fix is to provide a public accessor or mutator method that controls how the outside world reaches that data.
Common Misconceptions
publicandprivateare not the same as static versus instance. Access modifiers control who can see a member; they do not decide whether it belongs to the object or the class.- Making everything
publicis not "easier." It breaks encapsulation and lets outside classes change your data without any checks. Keep instance variablesprivate. - Each object gets its own copy of the instance variables. Two objects of the same class do not share their instance variable values.
privatedoes not mean a method can never be used. It means the method can only be called from inside the same class, often as a helper for public methods.- A
privateinstance variable is still reachable from inside its own class. Encapsulation hides data from other classes, not from the class's own methods. - Writing accessor and mutator methods is how outside classes safely read or change private data. Returning that data through a controlled method is the point of encapsulation.
Related AP Computer Science A Guides
Vocabulary
The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.Term | Definition |
|---|---|
access constraints | Restrictions that control which classes and methods can access or modify a class, data, constructors, or methods. |
attribute | The data or properties that define the state of an object or class. |
behavior | The methods or actions that an object or class can perform. |
constructors | Special methods used to initialize objects of a class, typically designated as public in this course. |
data encapsulation | A technique in which the implementation details of a class are kept hidden from external classes to protect internal structure and behavior. |
instance variable | A variable that belongs to an object and can be accessed throughout the class, as opposed to a local variable that is limited to a specific block of code. |
method | A named block of code that only runs when it is called, allowing programmers to reuse code and organize programs into logical sections. |
private | A keyword that restricts access to a class, data, constructor, or method to only the declaring class. |
public | A keyword that allows access to a class, data, constructor, or method from classes outside the declaring class. |
visibility constraints | Specifications that determine whether classes, data, constructors, and methods are visible and accessible to external classes. |