Topic 3.3 Notes – Anatomy of a Class
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
publicin 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:
| Instance | Static |
|---|---|
| Belongs to object | Belongs to class |
| Separate copy per object | One shared copy |
| Called on object | Called on class |
| Can access instance + static | Can 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.