# AP® Computer Science A 3.3 Anatomy of a Class

Source: https://lockincs.com/ap-csa/unit-3/3-3-anatomy-of-a-class/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 3.3.A, 3.3.A.1, 3.3.A.2, 3.3.A.3, 3.3.A.4, 3.3.A.5, 3.3.A.6

A class is declared with public class Name and contains private instance variables, public constructors and methods. Data encapsulation keeps the implementation details of a class hidden: private restricts access to the declaring class, while public allows access from other classes. Each object has its own copy of every instance variable.

## What you will learn

- 3.3.A Develop code to designate access and visibility constraints to classes, data, constructors and methods.

## 1\. The parts of a class

In this course, a class is always designated `public` and is declared with the keyword `class` 3.3.A.2. Inside, a typical class has three kinds of parts:

| Part | What it does | Access in AP® code |
| --- | --- | --- |
| Instance variables | Store the attributes of each object | `private` 3.3.A.5 |
| Constructors | Set the initial values when an object is created | always `public` 3.3.A.3 |
| Methods | Define the behaviors of the object | `public`, or `private` for helpers 3.3.A.6 |

Here is a complete class in the AP® free-response “class design” style. Read the comments: they label each part.

```
public class LibraryBook
{
    // ----- instance variables: private -----
    private String title;
    private int pages;
    private boolean checkedOut;

    // ----- constructor: public, no return type -----
    public LibraryBook(String bookTitle, int numPages)
    {
        title = bookTitle;
        pages = numPages;
        checkedOut = false;
    }

    // ----- accessor methods: public -----
    public String getTitle()
    {
        return title;
    }

    public boolean isCheckedOut()
    {
        return checkedOut;
    }

    // ----- mutator methods: public -----
    public void checkOut()
    {
        checkedOut = true;
    }

    public void returnBook()
    {
        checkedOut = false;
    }

    public void setPages(int numPages)
    {
        if (numPages > 0)
        {
            pages = numPages;
        }
    }

    /** Returns the number of days needed to read the book
     *  at pagesPerDay pages a day. Precondition: pagesPerDay > 0 */
    public int daysToRead(int pagesPerDay)
    {
        return divideRoundUp(pages, pagesPerDay);
    }

    // ----- helper method: private, used only inside the class -----
    private int divideRoundUp(int a, int b)
    {
        return (a + b - 1) / b;
    }

    // ----- toString: returns a String that describes the object -----
    public String toString()
    {
        return title + " (" + pages + " pages)";
    }
}
```

Client code in another class:

```
LibraryBook book = new LibraryBook("Ocean Tides", 250);
book.checkOut();
System.out.println(book.getTitle() + " out: " + book.isCheckedOut());
System.out.println(book.daysToRead(30));
System.out.println(book);
```

Output:

```
Ocean Tides out: true
9
Ocean Tides (250 pages)
```

**Background:** the last line works because `println` calls the object’s `toString` method. You will see `toString` in class code, but writing your own `toString` (overriding it) is outside the AP® exam, so the exam will not ask you to write one.

## 2\. Encapsulation: public and private

**Data encapsulation** is a technique in which the implementation details of a class are kept hidden from external classes 3.3.A.1. Two keywords control access to classes, data, constructors and methods 3.3.A.1:

-   `private` restricts access to the **declaring class** (the class where it is written) 3.3.A.1.
-   `public` allows access from **classes outside** the declaring class 3.3.A.1.

The class above hides *how* it stores its data. A client cannot see that `checkedOut` is a `boolean`; it only sees the behaviors `checkOut`, `returnBook` and `isCheckedOut`. The class could later store the status another way without breaking any client.

## 3\. Each object has its own instance variables

**Instance variables** belong to the object, and each object has its own copy of each variable 3.3.A.4.

```
LibraryBook a = new LibraryBook("Ocean Tides", 250);
LibraryBook b = new LibraryBook("Desert Stars", 120);
a.checkOut();
System.out.println(a.isCheckedOut() + " " + b.isCheckedOut());
System.out.println(a + " / " + b);
```

Output:

```
true false
Ocean Tides (250 pages) / Desert Stars (120 pages)
```

Memory after the code runs:

| Variable | Object it refers to: title | pages | checkedOut |
| --- | --- | --- | --- |
| `a` | “Ocean Tides” | 250 | true |
| `b` | “Desert Stars” | 120 | false |

Calling `a.checkOut()` changed only the `checkedOut` that belongs to `a`.

## 4\. Keep instance variables private

Access to attributes should be kept inside the class to accomplish encapsulation, so it is good practice to make instance variables `private` unless the class specification says otherwise 3.3.A.5. Then the only way to change `pages` is through `setPages`, which refuses bad values.

A client that tries to reach a private instance variable directly does not compile:

```
LibraryBook book = new LibraryBook("Ocean Tides", 250);
book.pages = -5;
```

Compiler says:

```
error: pages has private access in LibraryBook
```

The public mutator is the correct way in, and it protects the data:

```
LibraryBook book = new LibraryBook("Ocean Tides", 250);
book.setPages(-5);
System.out.println(book);
book.setPages(300);
System.out.println(book);
```

Output:

```
Ocean Tides (250 pages)
Ocean Tides (300 pages)
```

## 5\. Public and private methods

Access to behaviors can be internal or external to the class 3.3.A.6. A `public` method can be called from inside the class or from other classes. A `private` method can only be called inside the class 3.3.A.6.

In `LibraryBook`, the public method `daysToRead` calls the private helper `divideRoundUp`. That is allowed because the call is inside the class. A client calling the helper does not compile:

```
LibraryBook book = new LibraryBook("Ocean Tides", 250);
System.out.println(book.divideRoundUp(250, 30));
```

Compiler says:

```
error: divideRoundUp(int,int) has private access in LibraryBook
```

**Watch out:** choose `private` for a method when it is a small step that only the class itself needs, and `public` when it is a behavior that the class specification promises to other classes.

## Common mix-ups

- Not true: private means other objects of the same class cannot see the data. private restricts access to the declaring class. Code written inside the class can use the private data; code in other classes cannot.
- Not true: All objects of a class share the same instance variables. Instance variables belong to the object. Each object has its own copy, so changing one book's title does not change another book's title.
- Not true: Methods should always be private, like instance variables. Methods that other classes need (the behaviors of the object) are public. Helper methods used only inside the class can be private.

## In 30 seconds

- In this course, classes are public and declared with the keyword class; constructors are always public.
- Data encapsulation hides the implementation details of a class from external classes.
- private restricts access to the declaring class; public allows access from outside classes.
- Instance variables belong to the object: each object has its own copy.
- Make instance variables private unless the specification says otherwise. Methods can be public or private.

## Checkpoint: 5 practice questions

### Question 1

Consider the following class. The code segment below the class appears in a method in another class. Which of the following can replace `/* missing code */` without causing a compile-time error?

```java
public class Thermostat
{
    private int target;

    public Thermostat(int start)
    {
        target = start;
    }

    public int getTarget()
    {
        return target;
    }

    public void raise()
    {
        target++;
    }

    private boolean tooHot()
    {
        return target > 28;
    }
}

Thermostat t = new Thermostat(21);
/* missing code */
```

- A. `t.target = 22;`
- B. `boolean hot = t.tooHot();`
- C. `t.raise();`
- D. `int x = t.target + 1;`

**Answer: C.** `raise` is public, so another class can call it. `target` is a private instance variable, so `t.target` does not compile in another class, whether it is assigned or read. `tooHot` is a private method, so it can only be called from inside `Thermostat`.

### Question 2

A `Wallet` class needs an attribute that stores the amount of money in each wallet. The specification does not say anything about access. Which declaration best follows the principle of encapsulation?

- A. `public double balance;`
- B. `private double balance;`
- C. `private static double balance;`
- D. `public static double balance;`

**Answer: B.** Instance variables should be private unless the specification says otherwise, so the class keeps control of its data. A public variable lets any class change the balance directly. Adding `static` makes it a class variable shared by all wallets, so every wallet would have the same balance.

### Question 3

Consider the following class. The code segment below the class appears in a method in another class. What is printed as a result of executing the code segment?

```java
public class Thermostat
{
    private int target;

    public Thermostat(int start)
    {
        target = start;
    }

    public int getTarget()
    {
        return target;
    }

    public void raise()
    {
        target++;
    }
}

Thermostat a = new Thermostat(20);
Thermostat b = new Thermostat(20);
a.raise();
a.raise();
b.raise();
System.out.println(a.getTarget() + " " + b.getTarget());
```

- A. 23 23
- B. 21 21
- C. 22 22
- D. 22 21

**Answer: D.** Each object has its own copy of the instance variable `target`. `a` is raised twice (20 to 22) and `b` once (20 to 21). The option 23 23 treats `target` as if it were shared by both objects, and 22 22 or 21 21 ignore that the two objects were raised a different number of times.

### Question 4

The following method is added inside the `Thermostat` class from question 1, which has a private method `tooHot`. Which of the following best describes the result?

```java
public boolean needsCooling()
{
    return tooHot();
}
```

- A. It does not compile, because `tooHot` is private
- B. It compiles only if `needsCooling` is also declared `private`
- C. It compiles, because code in the declaring class can call its private methods
- D. It compiles, but calling it from another class causes a run-time error

**Answer: C.** `private` restricts access to the declaring class, and `needsCooling` is written inside `Thermostat`, so it may call `tooHot`. The access of the calling method does not matter. Because `needsCooling` is public, other classes can call it normally; access problems are found by the compiler, never at run time.

### Question 5

Which of the following correctly shows a class header and a constructor header for a `Bike` class, following the conventions of this course?

- A. `public class Bike` and `public Bike(int gears)`
- B. `private class Bike` and `public Bike(int gears)`
- C. `public class Bike` and `private Bike(int gears)`
- D. `public class Bike` and `public void Bike(int gears)`

**Answer: A.** In this course, classes are designated public and declared with the keyword `class`, and constructors are always public. A constructor has no return type: writing `void` turns it into an ordinary method named `Bike`, which is not a constructor. Private classes and private constructors are not used in this course.

## Questions students ask

**What are the parts of a class in Java?**

A class header (public class Name), instance variables that store each object's attributes, constructors that set the initial state, and methods that define the behaviors.

**Why are instance variables private in AP® Computer Science A?**

Making them private accomplishes encapsulation: only the class's own code can change the data, so the class controls its values. Other classes use public methods instead.

**Can a private method be called?**

Yes, but only from code inside the same class. A public method of the class can call a private helper method. Code in another class that calls it does not compile.

**What does encapsulation mean?**

Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes, mainly by making instance variables private.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
