Skip to main content

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
  • private
  • public
  • 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 always public and declared with the keyword class.
  • 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:

  • private means only code inside this class can use it.
  • public means 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.

  1. Example 1

    Using the class from outside

    Using the GiftCard class 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 solution
    1. Step 1: The constructor sets balance to 50 and declined to 0.
    2. Step 2: spend(20): 20 is at most 50, so the balance drops to 30 and the method returns true.
    3. Step 3: spend(40): 40 is more than 30, so the balance doesn't change, declined becomes 1, and it returns false.
    4. Step 4: The last line prints the balance and the number of declined purchases.

    Answer: Three lines: true, false, then 30 1.

  2. 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 solution
    1. Step 1: The first line calls the public constructor, which is allowed.
    2. Step 2: The second line tries to use balance directly. balance is private, so only code inside GiftCard may use it.
    3. 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 because balance is private.

Common mistakes

  • Making instance variables public. On the free-response question, instance variables should be private.
  • 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: private instance 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

  • AP CSA - Unit 5: Writing Classes – Lesson 1: Anatomy of a Class

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • AP CS A 5.1- Anatomy of Classes

    CodeHSWatch on YouTube (opens in a new tab)

  • Java encapsulation 💊

    Bro CodeWatch on YouTube (opens in a new tab)

  • Encapsulation in Java Tutorial #83

    Alex LeeWatch on YouTube (opens in a new tab)

  • Introduction to Abstraction, Ethics, and Data Encapsulation [APCSA 3.1 - 3.3]

    LearnAPCSAWatch on YouTube (opens in a new tab)

  • 2026 AP Computer Science A Exam Review - Exploring FRQ 2: Class Design

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 3.3 Anatomy of a Class. Pick an answer to see if you got it, and why.

Code
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;
    }
}
Question 1 of 4

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?

Question 2 of 4

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());

Question 3 of 4

Which of the following best explains why isSenior is declared private?

Question 4 of 4

Which of the following is the best reason to declare a class's instance variables private?

0 of 4 answered