Lesson 3.4: Constructors
Lesson 3.4: Constructors
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.
Practice Questions
Planet with one String parameter?public Planet(String name) { this.name = name; }
public void Planet(String name) { this.name = name; }
private Planet(String name) { this.name = name; }
public planet(String name) { this.name = name; }
public Rocket(String fuel) { fuel = fuel; }
fuel = fuel is self-assignment; instance variable stays null
this.fuel = fuel;.private int row; private int col; private String color;. After new Tile();?public class Gear {
private int teeth;
public Gear(int t) { t = this.teeth; }
}
this.teeth = t;
this.teeth is invalid in a constructor
t = this.teeth reads uninitialised teeth (0) into parameter. teeth stays 0.public Book() sets title="Unknown"; public Book(String t, int p). Which is TRUE?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?
Bag b = new Bag(5.5); where constructor does weight = weight; (both named weight). Instance variable value?Mastery: Constructors
Coin c = new Coin(25); where constructor only initializes value (not metal), what is metal?public class Vault {
private int code;
public void Vault(int c) { code = c; }
}
code = c is backwards
void Vault makes it a regular method; Vault objects use default constructor with code=0
Tank to default to capacity=100 when created with no arguments. Which constructor works?public Tank() { capacity = 100; }
public Tank() { int capacity = 100; }
public void Tank() { capacity = 100; }
private Tank() { capacity = 100; }
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]