AP CSA Unit 3: Class Creation - Complete Study Guide (2025)
AP Computer Science A – Unit 3: Class Creation (2025)
In Unit 3, you move from using existing classes (like String and Scanner) to creating your own classes. You’ll design instance variables, write constructors, implement methods, and understand how objects store and manage state.
📘 3.1 Unit Overview & Exam Context
According to the current AP CSA Course and Exam Description, Unit 3: Class Creation is worth about 10–18% of the multiple-choice exam. It also plays a huge role in FRQs: many questions expect you to read, modify, or write class code.
In this unit, you’ll learn to:
- Understand the relationship between classes and objects
- Declare instance variables to represent object state
- Write constructors to initialize new objects
- Implement methods (accessors, mutators, helper methods)
- Use encapsulation and data hiding
- Use the
thiskeyword correctly - Draw and interpret simple UML-style class diagrams
- Trace code that creates and manipulates objects
🏗 3.2 What Is a Class? What Is an Object?
In Java, a class defines:
- What data an object of that type stores (its fields / instance variables)
- What behaviors it can perform (its methods)
An object is a specific instance of a class, created with new.
Simple Example: A Dog Class
public class Dog {
// instance variables (fields)
private String name;
private int age;
// constructor
public Dog(String n, int a) {
name = n;
age = a;
}
// method
public void bark() {
System.out.println(name + " says: Woof!");
}
}
Creating and Using Dog Objects
Dog d1 = new Dog("Max", 3);
Dog d2 = new Dog("Luna", 5);
d1.bark(); // Max says: Woof!
d2.bark(); // Luna says: Woof!
🔐 3.3 Instance Variables & Encapsulation
Instance variables (also called fields) hold the state of each object. Every object of a class has its own copy of these variables.
Declaring Instance Variables
public class BankAccount {
// state / attributes
private String owner;
private double balance;
}
Why private?
- We usually declare instance variables as
private. - This is called encapsulation or data hiding.
- Other classes cannot directly change these fields.
- Instead, they must use public methods we provide.
Accessors & Mutators
To read or modify private fields, we write accessor and mutator methods:
public class BankAccount {
private String owner;
private double balance;
// accessor (getter)
public double getBalance() {
return balance;
}
// mutator (setter)
public void deposit(double amount) {
balance += amount;
}
}
Accessors normally:
- Have
publicvisibility - Return a value (return type is NOT
void) - Often start with
getoris(e.g.getBalance(),isEmpty())
Mutators normally:
- Have
publicvisibility - Return type is often
void - Change the object’s state
- Often start with
setor a verb (e.g.setBalance(),addItem())
🏗 3.4 Constructors – Building New Objects
A constructor is a special method that runs when you create a new object using new. Its job is to initialize the object’s instance variables.
Constructor Rules
- Has the same name as the class
- Has no return type (not even
void) - Can take parameters
- Is called automatically when you use
new
Example: Student Class
public class Student {
private String name;
private int gradYear;
// constructor
public Student(String n, int g) {
name = n;
gradYear = g;
}
public String getName() {
return name;
}
}
Using the Constructor
Student s1 = new Student("Alex", 2025);
Student s2 = new Student("Jordan", 2026);
System.out.println(s1.getName()); // Alex
Default Constructor
If you do not write any constructor, Java automatically provides a default constructor with no parameters that sets fields to default values (0, false, null).
But if you write any constructor (with parameters), Java does not create the no-argument one automatically.
public class Car {
private String model;
private int year;
// one constructor
public Car(String m, int y) {
model = m;
year = y;
}
}
// Later:
Car c = new Car(); // ❌ compile-time error, no matching constructor
new ClassName() unless that exact constructor exists.
⚙️ 3.5 Methods – Accessors, Mutators & Helper Methods
Methods define what an object can do. We usually categorize them into:
- Accessors – return information about the object (do not modify)
- Mutators – change the object’s state
-
Helper methods – support internal logic, may be
privateorpublic
Example: Rectangle Class
public class Rectangle {
private int width;
private int height;
// constructor
public Rectangle(int w, int h) {
width = w;
height = h;
}
// accessor
public int getWidth() {
return width;
}
// mutator
public void setWidth(int w) {
width = w;
}
// helper method (computed value)
public int getArea() {
return width * height;
}
}
Calling Methods
Rectangle r = new Rectangle(4, 5); System.out.println(r.getArea()); // 20 r.setWidth(10); System.out.println(r.getArea()); // 50
r.getArea() or r.setWidth(10) has on state and output.
📌 3.6 The this Keyword & Parameter Shadowing
Sometimes, constructor or method parameters have the same name as instance variables. In that case, we use this to refer to the current object’s fields.
Without this (Buggy)
public class Player {
private String name;
public Player(String name) {
name = name; // ❌ assigns parameter to itself, field unchanged
}
}
In this example, both the parameter and the field are named name. The assignment name = name; just copies the parameter onto itself. The instance variable remains null.
Correct Version Using this
public class Player {
private String name;
public Player(String name) {
this.name = name; // ✅ assigns parameter to the field
}
}
this can make your code clearer, especially when parameter names match field names. It’s also common in provided class code.
➕ 3.7 Multiple Constructors (Overloading Basics)
A class can have more than one constructor, as long as they have different parameter lists. This is called overloading.
Example: Book Class
public class Book {
private String title;
private int pages;
// full constructor
public Book(String t, int p) {
title = t;
pages = p;
}
// overloaded constructor with default pages
public Book(String t) {
title = t;
pages = 100; // default
}
}
Using the Constructors
Book b1 = new Book("Java Guide", 250);
Book b2 = new Book("Short Story");
AP CSA doesn’t dive deep into all method overloading complexities, but you should:
- Recognize when a class has multiple constructors
- Know which one will be used for a given
newexpression - Understand how different constructors initialize state differently
📐 3.8 UML-Style Class Diagrams (Lightweight)
The AP exam may show simplified class diagrams similar to UML. You don’t have to memorize full UML syntax, but you should be able to:
- Recognize class name, fields, and methods
- See parameter types and return types
- Translate diagrams to code and vice versa
Example Diagram (simplified text version)
Legend:
-
+→ public -
-→ private -
: typeafter a name → the data type - Methods may show parameters and return type
Translating to Java
public class BankAccount {
private String owner;
private double balance;
public BankAccount(String owner) { ... }
public void deposit(double amount) { ... }
public double getBalance() { ... }
}
🧬 3.9 Tracing Objects & Intro to Aliasing
When you assign one object variable to another, both variables refer to the same object in memory. This is called aliasing.
Example
BankAccount a1 = new BankAccount("Jordan");
BankAccount a2 = a1; // a2 refers to the same object as a1
a1.deposit(100);
System.out.println(a2.getBalance()); // ?
Output: 100.0
Because a1 and a2 refer to the same object, calling deposit(100) through a1 changes what a2 “sees” as well.
📤 3.10 Parameter Passing – Primitives vs. Object References
Java uses pass-by-value for all parameters. But what that value is depends on the type:
Primitives → value copied
public void changeValue(int x) {
x = 100; // modifies only the local copy
}
int a = 5;
changeValue(a);
System.out.println(a); // still 5
When you pass a primitive, the method receives a copy of the number.
Objects → reference copied
With objects, the reference is copied. Both variables point to the same object.
public void deposit(BankAccount acct) {
acct.deposit(50); // modifies the real object
}
BankAccount b = new BankAccount("Sam");
deposit(b);
System.out.println(b.getBalance()); // increased by 50
📍 3.11 Scope – Where Variables Live
Scope determines where a variable can be accessed.
1. Instance Variables
- Declared in the class body (outside methods)
- Accessible in every method of the class
- Exist as long as the object exists
2. Local Variables
- Declared inside methods or constructors
- Only exist inside that method
- Disappears after the method finishes
public void methodA() {
int x = 10; // local to methodA
}
public void methodB() {
System.out.println(x); // ❌ x does not exist here
}
🏛 3.12 Static vs. Instance Methods
Methods come in two types:
- Instance methods — called on objects
- Static methods — called on the class name
Instance Method Example
Dog d = new Dog("Max", 3);
d.bark();
Static Method Example
Math.sqrt(25);
Integer.parseInt("42");
🧱 3.13 Composition – “Has-a” Relationships
Composition occurs when a class stores objects of another class as its instance variables. This is also known as a has-a relationship.
Example: A Team Has Players
public class Team {
private Player captain;
private Player[] players;
public Team(Player cap, Player[] p) {
captain = cap;
players = p;
}
}
📐 3.14 Good Class Design (AP Style)
The College Board expects that you can interpret and write clean, well-designed classes. Common AP design principles:
✔ Encapsulate fields
Always private unless there is a clear reason not to.
✔ Provide meaningful method names
Methods should describe the action they perform: deposit(), getName(), addScore(), etc.
✔ Keep methods small and single-purpose
Each method should do exactly one thing clearly.
✔ Initialize all fields in the constructor
Avoid leaving fields with default (empty) values unless intentional.
✔ Be consistent with return types
Accessors return values. Mutators are usually void.
✔ Guard against invalid data when needed
public void setAge(int a) {
if (a >= 0) {
age = a;
}
}
- correct method headers
- using
thiscorrectly - initializing fields properly
- returning correct values
- avoiding direct field access
🧪 3.15 Tracing Class Code – AP Style Problems
These are the kinds of questions you will see on MCQs and FRQs.
Example 1
public class Counter {
private int count;
public Counter(int c) {
count = c;
}
public void addOne() {
count++;
}
public int getCount() {
return count;
}
}
Counter a = new Counter(5);
Counter b = a;
a.addOne();
System.out.println(b.getCount());
Output: 6
a and b reference the same Counter object.Example 2
public class Box {
private int size;
public Box(int s) {
size = s;
}
public void grow() {
size *= 2;
}
}
Box x = new Box(3);
x.grow();
x.grow();
System.out.println(x.getSize());
Output: 12
⚠️ 3.16 Common Mistakes in Class Design
- Forgetting to assign constructor parameters to fields
- Using
name = name;instead ofthis.name = name; - Not initializing fields in constructor
- Returning
voidwhen a value is required - Directly modifying fields instead of using mutator methods
- Writing methods that accidentally shadow instance variables
- Misunderstanding aliasing when passing objects to methods
📝 Unit 3 Quiz – Class Creation (10 Questions)
Question 1
Which keyword refers to the current object?
- A. self
- B. object
- C. this
- D. current
Answer: C
Question 2
Which method type is most likely to change an object's state?
- A. accessor
- B. mutator
- C. static
- D. helper
Answer: B
Question 3
What is required for a method to be considered a constructor?
- A. Must return a value
- B. Must be static
- C. Same name as class
- D. Must have no parameters
Answer: C
Question 4
Which of the following is an example of encapsulation?
- A. Using
publicfields - B. Using
privatefields with public getters - C. Writing multiple constructors
- D. Overriding methods
Answer: B
Question 5
What is printed?
class Test {
private int x;
public Test(int n) { x = n; }
public void add(int n) { x += n; }
public int get() { return x; }
}
Test t = new Test(5);
t.add(3);
System.out.println(t.get());
- A. 3
- B. 5
- C. 8
- D. Error
Answer: C
Question 6
Which statement about parameter passing is correct?
- A. Java is pass-by-reference
- B. Java is pass-by-value
- C. Primitives are pass-by-reference
- D. Objects are copied entirely on method call
Answer: B
Question 7
Given: BankAccount a2 = a1;, which is true?
- A. a2 is a new copy of a1
- B. a2 refers to the same object as a1
- C. a2 cannot modify the object
- D. a2 is null
Answer: B
Question 8
What is the return type of a constructor?
- A. void
- B. int
- C. same as class name
- D. none
Answer: D Constructors have no return type—not even void.
Question 9
In a UML diagram, a minus sign (-) before a field means:
- A. protected
- B. abstract
- C. private
- D. static
Answer: C
Question 10
Which best describes composition?
- A. A class inherits another class
- B. A class uses another class as a helper
- C. A class contains objects of another class
- D. A class overrides methods of another class
Answer: C
🚀 Next Steps: Move on to Unit 4 – Data Collections
Now that you can build and design your own classes, it's time to learn how to manage multiple pieces of data efficiently. Unit 4 dives into arrays, ArrayLists, searching, sorting, and traversal patterns.
Add your internal link here:
Continue to AP CSA Unit 4: Data Collections →