Topic 3.4 Notes – Constructors
1. What Constructors Are and What They Do
When you write:
Student s = new Student("Maya", 11);
Java does four things in order:
- Allocates memory for a new
Studentobject - Creates instance variables inside that object
- Runs the constructor body
- 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→0double→0.0boolean→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:
StringArrayList- 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→ 0double→ 0.0boolean→ 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
ArrayListor 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.