5m left·0%
Reading Time: 5 min
Last Updated: March 23, 2026
Main Ideas: 4
Reading Time: 5 min
Last Updated: March 23, 2026
Main Ideas: 4

Topic 3.3 Notes – Anatomy of a Class

Verified for 2027 AP® Computer Science A Exam
Read aloud
A Java class defines a new type of object. It brings together the data each object stores and the actions each object can perform. Topic 3.3 is about the internal structure of a class and how access modifiers control what other classes are allowed to see or use.

1. What a Java Class Is

A class is a blueprint. When you write:

public class Player {

    private String name;
    private int score;

    public Player(String n) {
        name = n;
        score = 0;
    }

    public void addPoints(int pts) {
        score += pts;
    }

    public int getScore() {
        return score;
    }
}

You are defining three core pieces:

  • State → stored in instance variables (name, score)
  • Behavior → defined by methods (addPoints, getScore)
  • Construction → handled by the constructor (Player(...))

A few AP-specific rules:

  • Classes are declared public class ClassName
  • Constructors are public
  • Instance variables belong to each object

If you create three Player objects, you get three separate copies of name and score.

2. Access Modifiers and Encapsulation

The big idea here is encapsulation. That means hiding internal details and controlling access.

Two keywords matter:

private

  • Only accessible inside the same class
  • Used for:
    • Instance variables (almost always)
    • Helper methods

public

  • Accessible from other classes
  • Used for:
    • The class itself (in this course)
    • Constructors
    • Methods that form the class interface

Encapsulation means outside code cannot directly change your data.

✔️ Good design:

private int score;

public void setScore(int newScore) {
    if (newScore >= 0) {
        score = newScore;
    }
}

❌ Poor design:

public int score;   // anyone can change this to anything

If score is public, another class could do:

player.score = -500;

That breaks your logic. With private, all changes must go through your method.

On FRQ 2, graders immediately check whether your instance variables are private. Forgetting that costs easy points.

3. Instance Variables, Constructors, and Methods

These three pieces work together.

Instance Variables

  • Declared inside the class, outside methods
  • Store object data
  • Each object has its own copy
  • Should be private

Example:

private String title;
private int pages;

Every Book object would have its own title and pages.

Constructors

A constructor:

  • Has the same name as the class
  • Has no return type (not even void)
  • Initializes instance variables
  • Is public in AP CSA
public Book(String t, int p) {
    title = t;
    pages = p;
}

Common mistake:

public void Book(String t)   // ❌ This is NOT a constructor

If you add void, it becomes a regular method.

Methods

Methods define behavior. They can be:

  • Public methods
    • Called from other classes
    • Form the class interface
  • Private methods
    • Only used inside the class
    • Often helper methods
private boolean isValidPages(int p) {
    return p > 0;
}

Private helper methods are common in well-written FRQs. They make your logic cleaner and show strong design.

4. Instance vs Static Members

This is tested a lot in multiple choice.

Instance Members

  • Belong to an object
  • Accessed using an object reference
  • Each object has its own copy

Static Members

  • Belong to the class itself
  • Shared across all objects
  • Accessed with ClassName.methodName()

Comparison:

InstanceStatic
Belongs to objectBelongs to class
Separate copy per objectOne shared copy
Called on objectCalled on class
Can access instance + staticCan directly access static only

Key rule:
A static method cannot directly use instance variables.

If you see a static method trying to access name or score without an object, that’s a compile error.

Key Takeaways

Every object gets its own copy of instance variables.
Constructors have no return type and must initialize object state.
Instance variables should almost always be private for encapsulation.
Public methods define what other classes are allowed to do.
Static members belong to the class and cannot directly access instance variables.
On FRQ 2, missing private on instance variables is one of the most common lost points.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining