6m left·0%
Reading Time: 6 min
Last Updated: July 29, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: July 29, 2026
Main Ideas: 5

Topic 3.4 Notes – Constructors

Verified for 2027 AP® Computer Science A Exam
Read aloud
Constructors are special methods that run when you create an object with new. Their job is to initialize the object’s instance variables so the object starts in a valid state. This topic is about how objects get their initial state and how Java handles constructors behind the scenes.

1. What Constructors Are and What They Do

When you write:

Student s = new Student("Maya", 11);

Java does four things in order:

  1. Allocates memory for a new Student object
  2. Creates instance variables inside that object
  3. Runs the constructor body
  4. Returns a reference to the object and stores it in s

A constructor is a special method that runs automatically in step 3.

Object State

An object’s state is the collection of its instance variables and their values at a given time.

If a class looks like this:

private String name;
private int gradeLevel;

Then a Student has a name and has a gradeLevel.
That’s the has-a relationship. Instance variables define what an object owns.

Here’s what that idea looks like for new Student("Maya", 11):

The constructor’s job is to set up that state correctly so the object starts with the right values.

Constructor Syntax Rules

You need these memorized:

  • Same name as the class
  • No return type (not even void)
  • Usually public
  • Called automatically with new

If you accidentally write:

public void Student() { }

That is a regular method. Not a constructor. Classic MC trap.

2. Declaring and Initializing Instance Variables

Before you even think about the constructor, your instance variables must be declared at the top of the class:

private String name;
private int age;
private ArrayList<String> courses;

Then the constructor assigns them values.

Primitive Types

Default values (if you do nothing):

  • int → 0
  • double → 0.0
  • boolean → false

Java will assign those automatically, but on FRQs you should explicitly initialize variables so the object’s starting state is intentional.

Reference Types

Default value = null.

That includes:

  • String
  • ArrayList
  • Any object type

If you don’t initialize a reference variable and later call a method on it, you get a NullPointerException.

Example:

public Student(String studentName) {
    name = studentName;
    courses = new ArrayList<String>();
}

Without that new ArrayList<String>(), calling courses.add() would crash.

Using Constructor Parameters

Parameters let the person creating the object control the starting state:

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

Why this.name?

Because otherwise:

name = name;

would assign the parameter to itself. The instance variable would stay unchanged.

3. Types of Constructors

No-Parameter Constructor

public Timer() {
    seconds = 0;
    isRunning = false;
}

Creates a predictable default object.

Parameterized Constructor

public Timer(int startSeconds) {
    seconds = startSeconds;
    isRunning = false;
}

Lets you customize the initial state.

On FRQs, this is the most common form.

Java’s Automatic Default Constructor

If you write no constructor at all, Java provides:

public ClassName() { }

Instance variables get their default values:

  • int → 0
  • double → 0.0
  • boolean → false
  • reference types → null

Here’s the rule that shows up on tests:

If you write any constructor, Java does not provide the no-arg constructor anymore.

So this will cause a compile error:

public Point(int x, int y) { }
Point p = new Point();  // error

4. Mutable Object Parameters and Defensive Copying

This is subtle but very important.

Suppose your constructor receives an ArrayList:

public Student(ArrayList<String> list) {
    courses = list;
}

Now both courses and list refer to the same object.

If code outside the class modifies list, it also changes the student’s internal state.

That’s shared aliasing.

Instead, make a defensive copy:

courses = new ArrayList<String>(list);

Now:

  • courses → new independent list
  • original list → separate object

This protects the object’s state from outside modification. On the AP exam, if a constructor receives a mutable object, copying it is the correct move.

5. Common Mistakes and Exam Traps

  • Writing a return type in a constructor
  • Forgetting to initialize ArrayList or other objects
  • Forgetting this. when parameters shadow instance variables
  • Not initializing every instance variable in the constructor
  • Assuming the no-arg constructor still exists after defining another one

FRQ graders look specifically for complete initialization. If one instance variable is left untouched, you lose points.

Key Takeaways

A constructor runs automatically when new is used and sets the object’s initial state.
Instance variables define the object’s state and create a has-a relationship.
Constructors have no return type and must match the class name exactly.
If you write any constructor, Java does not generate a no-arg constructor for you.
Reference variables default to null, so collections must be created with new inside the constructor.
Use this.variableName when parameters have the same name as instance variables.
When a constructor receives a mutable object like an ArrayList, create a copy to prevent shared state.

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