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 this keyword correctly
  • Draw and interpret simple UML-style class diagrams
  • Trace code that creates and manipulates objects
Big Idea: A class is a blueprint. An object is a specific “thing” built from that blueprint. In AP CSA, you must be comfortable writing and using both.

 

 

🏗 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!
Class (blueprint): Dog - fields: name, age - behaviors: bark() Objects (built from the blueprint): d1 --> Dog("Max", 3) d2 --> Dog("Luna", 5)
AP questions often show a small class like this and ask about the effects of calling its methods or changing its instance variables.

 

 

🔐 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.
Encapsulation Principle: Objects manage their own data and expose only safe operations through methods. This makes code easier to understand, test, and maintain.

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 public visibility
  • Return a value (return type is NOT void)
  • Often start with get or is (e.g. getBalance(), isEmpty())

Mutators normally:

  • Have public visibility
  • Return type is often void
  • Change the object’s state
  • Often start with set or 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
On the AP exam, you must recognize the constructor, know what it initializes, and predict the state of new objects after they’re created.

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
If an FRQ shows a class with only one constructor that takes parameters, you cannot create objects with 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 private or public

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
AP will often show you a class, then ask what result a call like 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.name --> instance variable name --> constructor parameter
On AP FRQs, using 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 new expression
  • Understand how different constructors initialize state differently
If the parameter list doesn’t match any constructor exactly (in type, number, order), the code will not compile.

 

 

📐 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)

------------------------- | BankAccount | ------------------------- | - owner : String | | - balance : double | ------------------------- | + BankAccount(String) | | + deposit(double) | | + getBalance() : double | -------------------------

Legend:

  • + → public
  • - → private
  • : type after 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() { ... }
}
Practice reading class diagrams and guessing what code they represent—this mirrors what you’ll do on AP questions.

 

 

🧬 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

a1 ---> [ BankAccount object ] a2 ---^ Both variables point to the same underlying account.

Because a1 and a2 refer to the same object, calling deposit(100) through a1 changes what a2 “sees” as well.

Aliasing is a favorite AP concept. You’ll see it in FRQs where multiple references to the same object are passed around and modified.

 

 

 

 

 

 

📤 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
For objects: originalVar ---> [ Object ] methodParam ----^
AP Exam Tip: Passing an object to a method allows that method to change the object’s state. Passing a primitive does NOT change the original variable.

 

 

📍 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
}
Local variables cannot be accessed outside their curly braces. Instance variables can be accessed anywhere inside the class.

 

 

🏛 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");
Rule: If it needs access to instance variables → make it an instance method. If it’s a general utility operation → make it static.

 

 

🧱 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;
    }
}
Team has-a Player captain has-a Player[] players
AP often tests your ability to trace interactions between related objects. If Class A stores a reference to Class B, modifying B affects A’s view too.

 

 

📐 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;
    }
}
On FRQs, points are awarded for:
  • correct method headers
  • using this correctly
  • 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

Both 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 of this.name = name;
  • Not initializing fields in constructor
  • Returning void when 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
Many FRQ deductions come from small mistakes in constructors or accessors. Always double-check: Did I assign the fields correctly?

 

 

📝 Unit 3 Quiz – Class Creation (10 Questions)

Question 1

Which keyword refers to the current object?

  • A. self
  • B. object
  • C. this
  • D. current

Question 2

Which method type is most likely to change an object's state?

  • A. accessor
  • B. mutator
  • C. static
  • D. helper

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

Question 4

Which of the following is an example of encapsulation?

  • A. Using public fields
  • B. Using private fields with public getters
  • C. Writing multiple constructors
  • D. Overriding methods

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

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

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

Question 8

What is the return type of a constructor?

  • A. void
  • B. int
  • C. same as class name
  • D. none

Question 9

In a UML diagram, a minus sign (-) before a field means:

  • A. protected
  • B. abstract
  • C. private
  • D. static

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

 

 

🚀 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 →

Contact form