Lesson 3.4: Constructors

Unit 3 · Lesson 3.4 · Code Mechanics

Lesson 3.4: Constructors

🕑 30–35 min· 8 Practice Questions· 4 Mastery Questions· Output Predictor + Bug Hunt

What You'll Learn

  • Write correctly structured constructors with proper initialization
  • Explain Java's default constructor and default variable values
  • Use this.varName = paramName; when names shadow each other
  • Identify reversed-assignment bugs
  • Write overloaded constructors

Key Vocabulary

Term Definition
constructor Special method that initializes a new object's state; same name as the class, no return type (EK 3.4.A.2)
object state Values of all instance variables at a given time (EK 3.4.A.1)
has-a relationship An object has its instance variables; e.g., a Car has an int mileage (EK 3.4.A.1)
default constructor No-parameter constructor Java provides when none is written; sets fields to default values (EK 3.4.A.4)
default value int: 0  |  double: 0.0  |  boolean: false  |  reference types: null (EK 3.4.A.5)
overloaded constructor Two or more constructors in the same class with different parameter lists
null Default value for any reference type that has not been initialized

CED EK 3.4.A.1–3.4.A.5

A constructor initializes object state when called with new (EK 3.4.A.2). If no constructor is written, Java provides a default no-arg constructor with default values (EK 3.4.A.4–3.4.A.5). When a mutable object is passed as a constructor parameter, the instance variable should be initialized with a copy of that reference (EK 3.4.A.3).

Constructor Syntax and Rules

public class Dog {
    private String name;
    private int age;

    // Same name as class, NO return type
    public Dog(String name, int age) {
        this.name = name;  // 'this.name' = instance variable
        this.age  = age;
    }
}
Constructor Rule Why It Matters
Same name as class Java identifies constructors by matching the class name
No return type (not even void) Adding a return type makes it a regular method
Usually public Allows creation from outside the class
Called with new Allocates memory and returns a reference
Initializes ALL instance variables Prevents unintended null/0 state

Default Values

Type Default Value
int, short, long 0
double, float 0.0
boolean false
Reference types (String, objects) null

Default Values in Practice

public class Box {
    private int width;
    private String label;
    // No constructor
}
Box b = new Box();
// b.width = 0, b.label = null

A String defaults to null. Calling any method on null throws a NullPointerException.

Overloaded Constructors

Two Constructors

public class Circle {
    private double radius;
    public Circle()           { radius = 1.0; }  // default
    public Circle(double r)   { radius = r;   }  // parameterized
}
Circle c1 = new Circle();       // radius = 1.0
Circle c2 = new Circle(5.5);    // radius = 5.5

AP Trap: Return Type Kills the Constructor

// BROKEN -- this is a METHOD named Dog, not a constructor
public void Dog(String name) { this.name = name; }

Adding void (or any return type) makes it a regular method. Java then uses the default constructor. No compile error, but the constructor doesn't exist.

AP Trap: Parameter Shadows Instance Variable

public Pen(String color) {
    color = color;  // self-assignment! instance var stays null
}

When parameter and instance variable share a name, use this.color = color;.

AP Trap: Backwards Assignment

public Car(String model) {
    model = this.model;  // reads null into parameter
}

Must be this.model = model;. The reversed version reads the uninitialized null and does nothing useful.

Real-World Connection: A constructor is like a hospital intake form: when a patient checks in (object created), the form (constructor) captures name, DOB, and insurance (instance variables) before the patient can be processed.

Summary

  • A constructor has the same name as the class and NO return type.
  • If no constructor is written, Java provides a default no-arg constructor with 0/0.0/false/null defaults.
  • When parameter and instance variable share a name, use this.varName = paramName;.
  • Reversed assignment (param = this.var;) leaves the instance variable uninitialized.
  • Overloaded constructors provide multiple ways to initialize an object.
Tier 2 · AP Practice

Practice Questions

MCQ 1
Which correctly declares a constructor for Planet with one String parameter?
A public Planet(String name) { this.name = name; }
B public void Planet(String name) { this.name = name; }
C private Planet(String name) { this.name = name; }
D public planet(String name) { this.name = name; }
A is correct. Valid: public, no return type, exact class name. B adds void. C is private. D uses wrong case.
MCQ 2
The following constructor has a bug. What is it?
public Rocket(String fuel) { fuel = fuel; }
⚠ Predict the answer before reading the options.
A Missing return type
B Constructor should be private
C fuel = fuel is self-assignment; instance variable stays null
D Should call super() first
C is correct. Fix: this.fuel = fuel;.
MCQ 3
A class has no written constructors. Which is TRUE?
A Won't compile; every class must declare a constructor
B Only a no-arg constructor call is valid; Java provides one automatically
C Parameterized constructor calls also work
D Default constructor sets variables to random values
B is correct. Java provides a default no-arg constructor. C is false. D is false.
MCQ 4
No constructor. Variables: private int row; private int col; private String color;. After new Tile();?
A row=1, col=1, color=""
B row=null, col=null, color=null
C row=0, col=0, color=null
D row=0, col=0, color=""
C is correct. int defaults to 0; String defaults to null (EK 3.4.A.5).
MCQ 5
Compiles but logically broken.
public class Gear {
    private int teeth;
    public Gear(int t) { t = this.teeth; }
}
⚠ Predict the answer before reading the options.
A Parameter must match instance variable name
B Constructor needs return type
C Assignment is backwards; should be this.teeth = t;
D this.teeth is invalid in a constructor
C is correct. t = this.teeth reads uninitialised teeth (0) into parameter. teeth stays 0.
MCQ 6
Two constructors: public Book() sets title="Unknown"; public Book(String t, int p). Which is TRUE?
A Won't compile; only one constructor allowed
B new Book() sets title to null
C new Book("Dune",412) creates Book with title "Dune" and pages 412
D new Book(412,"Dune") also works; Java reorders arguments
C is correct. Overloaded constructors valid. B is false; no-arg sets "Unknown". D is false; argument order is fixed.
MCQ 7
I. Adding void makes a valid void constructor
II. If none written, Java provides a default no-arg constructor
III. Default sets int to 0 and reference types to null
Which are TRUE?
A I only
B I and II only
C I and III only
D II and III only
D is correct. II (EK 3.4.A.4) and III (EK 3.4.A.5) are true. I is false.
MCQ 8
Bag b = new Bag(5.5); where constructor does weight = weight; (both named weight). Instance variable value?
A 5.5
B 0.0, self-assignment; instance variable stays at default
C null
D 5.5, Java auto-adds this.
B is correct. Self-assignment. Double default = 0.0. Java does NOT auto-add this.
PRACTICE WITH A GAME — CHOOSE ONE:
Output Predictor: Constructors
Predict based on constructor execution.
Question 1 of 3  ·  Score: 0

Done!
You got 0 of 3 correct.
Bug Hunt: Constructors
Click the buggy line, or 'No bug'.
Question 1 of 3  ·  Score: 0
No bug — code is correct
Done!
You got 0 of 3 correct.
Tier 3 · AP Mastery

Mastery: Constructors

MCQ 1
After Coin c = new Coin(25); where constructor only initializes value (not metal), what is metal?
A "" (empty string)
B "Unknown"
C "25"
D null
D is correct. metal is a String (reference type) not initialized in the constructor. Default = null (EK 3.4.A.5).
MCQ 2
The following class has a bug. What is it and what are its consequences?
public class Vault {
    private int code;
    public void Vault(int c) { code = c; }
}
A code = c is backwards
B void Vault makes it a regular method; Vault objects use default constructor with code=0
C Parameter should be named code, not c
D Missing accessor for code prevents compilation
B is correct. Adding void makes it a regular method. Java then provides the default no-arg constructor; code stays 0.
MCQ 3
Programmer wants Tank to default to capacity=100 when created with no arguments. Which constructor works?
A public Tank() { capacity = 100; }
B public Tank() { int capacity = 100; }
C public void Tank() { capacity = 100; }
D private Tank() { capacity = 100; }
A is correct. Public no-arg constructor assigning to the instance variable. B declares a local variable. C adds void. D is private.
MCQ 4
Which CORRECTLY describes the has-a relationship (EK 3.4.A.1)?
A A subclass has-a superclass because it extends it
B A method has-a return type
C An object has-a set of instance variables that define its state
D A constructor has-a class name because it matches
C is correct. EK 3.4.A.1: object has instance variables = its state. A describes inheritance. B and D describe structure, not the has-a relationship.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]