AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/3/3-3)
Unit 3 · Topic 3.3
3.3 Anatomy of a Class
Writing your own class starts with its anatomy: the header, private instance variables, public constructors and methods. This topic explains what each part does and why encapsulation, keeping data private, is the standard way to design a class. Changed in 2025: inheritance (writing subclasses) was cut from the course, so every class you write stands on its own.
Key terms
- encapsulation
privatepublic- instance variable
- class header
The parts of a class
Here's a complete class for a gift card that refuses purchases bigger than its balance and counts how many it has refused.
public class GiftCard
{
private int balance;
private int declined;
public GiftCard(int startBalance)
{
balance = startBalance;
declined = 0;
}
public boolean spend(int amount)
{
if (amount <= balance)
{
balance -= amount;
return true;
}
declined++;
return false;
}
public int getBalance()
{
return balance;
}
public int getDeclined()
{
return declined;
}
}
- The class header,
public class GiftCard. In this course, classes are alwayspublicand declared with the keywordclass. - Instance variables, declared inside the class but outside every method. They hold each object's data.
- A constructor, which has the same name as the class and no return type. It sets up a new object (3.4). Constructors in this course are always
public. - Methods, which give the object its behaviors (3.5).
Encapsulation
Encapsulation means keeping a class's inner details hidden from other classes. The keywords public and private control access:
privatemeans only code inside this class can use it.publicmeans code in any class can use it.
Why instance variables are private
Instance variables should be private unless the description says otherwise. Then the only way to change a gift card's balance is through spend, which enforces the rule that the balance never goes negative. If balance were public, any code could set it to a million.
Methods can be either. Public methods are how other classes use this one. A private method is a helper that only the class's own methods can call, useful for breaking up a long method without exposing extra pieces.
Each object gets its own copy of every instance variable. Spending from one card leaves every other card's balance alone.
From a description to code
Free-response Question 2 gives you a description of a class and a table of example calls. Turn the description into code in this order. First write the header. Next decide what each object must remember between method calls; those values become private instance variables. Then write the constructor so it sets all of them. Finally write each method with exactly the name, parameter types and return type the description gives.
If a value is only needed while one method runs, it should be a local variable in that method, not an instance variable.
Not on the exam
Since the 2025 course update, writing subclasses (inheritance) isn't part of AP Computer Science A. Every class you write stands on its own.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Using the class from outside
Using the
GiftCardclass above, what does this code in another class print?GiftCard card = new GiftCard(50); System.out.println(card.spend(20)); System.out.println(card.spend(40)); System.out.println(card.getBalance() + " " + card.getDeclined());Show the solutionHide the solution
- Step 1: The constructor sets
balanceto 50 anddeclinedto 0. - Step 2:
spend(20): 20 is at most 50, so the balance drops to 30 and the method returnstrue. - Step 3:
spend(40): 40 is more than 30, so the balance doesn't change,declinedbecomes 1, and it returnsfalse. - Step 4: The last line prints the balance and the number of declined purchases.
Answer: Three lines:
true,false, then30 1. - Step 1: The constructor sets
- Example 2
What compiles outside the class?
A different class contains this code. Does it compile?
GiftCard card = new GiftCard(50); card.balance = 1000;Show the solutionHide the solution
- Step 1: The first line calls the public constructor, which is allowed.
- Step 2: The second line tries to use
balancedirectly.balanceisprivate, so only code insideGiftCardmay use it. - Step 3: The compiler rejects the second line. To change the balance from outside, the class has to offer a public method, such as
spend.
Answer: No.
card.balance = 1000;is a compile error becausebalanceis private.
Common mistakes
- Making instance variables
public. On the free-response question, instance variables should beprivate. - Declaring instance variables inside the constructor. Then they're local variables that vanish when the constructor ends (3.8).
- Giving a constructor a return type, even
void. A constructor has no return type at all.
On the exam
- Since the 2025 course update, free-response Question 2 (Class Design) is worth 7 points instead of 9, and points are only earned, never taken away as penalties. You write the whole class: the header, private instance variables, a constructor and the required methods, usually matched to a table of example calls.
- Points on Question 2 depend on getting the details right:
privateinstance variables, a constructor that sets every one of them, and methods with the exact names, parameters and return types in the description.
Connected topics
Videos
Check yourself
4 questions on 3.3 Anatomy of a Class. Pick an answer to see if you got it, and why.
public class Pet
{
private String name;
private int age;
public Pet(String n, int a)
{
name = n;
age = a;
}
public int getAge()
{
return age;
}
public void haveBirthday()
{
age++;
}
private boolean isSenior()
{
return age >= 10;
}
}Assume Pet p = new Pet("Rex", 4); appears in a class other than Pet. Which of the following statements, appearing after it in the same class, compiles without error?
What is printed as a result of executing the following code segment, which appears in a class other than Pet?Pet a = new Pet("Rex", 9);
Pet b = new Pet("Tom", 9);
a.haveBirthday();
a.haveBirthday();
b.haveBirthday();
System.out.println(a.getAge() + " " + b.getAge());
Which of the following best explains why isSenior is declared private?
Which of the following is the best reason to declare a class's instance variables private?
0 of 4 answered