Creating & Initializing Objects: Constructors
Building an Object with new
A constructor initializes a new object's attributes when it's created. To build one, use new followed by the class name — that's actually a call to the constructor:
DriveMotor leftMotor = new DriveMotor(5); // 5 is the PWM channel it's plugged into
(DriveMotor is the team's own motor class from Lesson 4.4 — assume it exists.)
Constructors Can Be Overloaded
A class can offer more than one constructor — different ways to set up an object, distinguished by their parameter lists. This is the same overloading concept from methods (Lesson 4.1), applied to constructors. WPILib's Pose2d class (a robot's position and heading on the field) has several:
// no-argument constructor: defaults to (0, 0, facing 0°)
Pose2d start1 = new Pose2d();
// x, y, and heading, explicitly
Pose2d start2 = new Pose2d(1.0, 2.0, new Rotation2d());
Pose2d and Rotation2d are real WPILib classes, from the package org.wpilib.math.geometry; a pose's x and y are measured in meters.
Argument order matters — mixing up which number means what (is it x-then-y, or y-then-x?) produces a real, silent bug: the code compiles fine, but the object ends up with the wrong values.
Object References and null
A reference variable can be declared without pointing at any object yet — that's null, meaning "doesn't refer to anything."
DriveMotor intakeMotor = null; // no object yet
intakeMotor = new DriveMotor(7); // now it does
Calling a method on a null reference — one that still holds null because no object was ever assigned to it — throws a NullPointerException at runtime (more on this in the next lesson).
Constructor Signatures
Just like methods, a constructor has a signature: its name (always the same as the class) plus its parameter types. Look up a library class's constructors in its documentation (its API page's Constructor Summary — Lesson 3.2) to see what arguments it expects and in what order.
public class Pose2d {
public Pose2d() { /* not shown */ }
public Pose2d(double x, double y, Rotation2d rotation) { /* not shown */ }
}
Common Pitfalls
- Forgetting
new.DriveMotor leftMotor = DriveMotor(5);doesn't compile — creating an object always requiresnew. - Mixing up argument order. A constructor taking
(x, y, rotation)called as(rotation, x, y)either won't compile (wrong types) or, worse, compiles with the wrong values silently swapped. - Calling a method on a
nullreference.DriveMotor m = null;followed bym.setThrottle(0.5);compiles, but crashes with aNullPointerExceptionat runtime. (A local variable given no value at all — justDriveMotor m;— is different: Java won't compile code that readsmbefore it's assigned, reporting "variable m might not have been initialized.")
Key Takeaways
new ClassName(arguments)calls a constructor to build and initialize an object.- A class can have multiple overloaded constructors, differing in their parameter lists.
- A reference variable can be
null— pointing at no object at all — until it's assigned a real one withnew. - Calling a method on a
nullreference throws aNullPointerException. - Constructor argument order must match the constructor's signature exactly.
Derived from csawesome-2026: unit-1-using-objects-and-methods/1.13-creating-and-initializing-objects-constructors.md (CSA 1.13)