How does a constructor initialise a new object's instance variables, and what happens if you omit one?
Topic 5.2 Constructors: write constructors that initialise an object's instance variables from parameters, including overloaded and default constructors, and explain the default initial values.
A focused answer to AP CSA Topic 5.2, covering constructor syntax, initialising instance variables from parameters, the no-argument constructor, overloading, default values when a variable is not set, and the difference between a parameter and an instance variable, with a fully worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 5.2) wants you to write constructors: the special methods that run when an object is created with new, setting up its instance variables. You should initialise instance variables from parameters, write a no-argument constructor, overload constructors (several with different parameter lists), and know the default values an instance variable takes if a constructor leaves it unset.
Constructor syntax
public class Dog {
private String name;
private int age;
public Dog(String n, int a) { // constructor: same name, no return type
name = n;
age = a;
}
}
You create the object by calling the constructor with new: Dog d = new Dog("Rex", 3);. The arguments are matched to the parameters by position and type.
Parameters versus instance variables
A parameter is a local variable that exists only during the constructor call; an instance variable belongs to the object and persists. The constructor's work is usually to copy each parameter into the matching instance variable. When the parameter and instance variable share a name, you disambiguate with this (Topic 5.9); using different names (n, a) avoids the issue.
Overloading and the no-argument constructor
public Dog() { // no-argument constructor
name = "unknown";
age = 0;
}
If you write no constructor at all, Java provides a hidden default no-argument constructor. But as soon as you write any constructor, that automatic one disappears - so if you still want a no-argument version, you must write it yourself.
Default initial values
If a constructor leaves an instance variable unset, it holds its type's default: int is 0, double is 0.0, boolean is false, and any object type (like String) is null. Relying on defaults is risky for object types, because calling a method on a null field throws a NullPointerException.
Try this
Q1. State two features that distinguish a constructor from an ordinary method. [1 point]
- Cue. It has the same name as the class, and it has no return type.
Q2. Explain what value a private String name; field holds if the constructor never assigns it, and why this is risky. [2 points]
- Cue. It defaults to
null(the default for object types); calling a method on it, such asname.length(), would throw a NullPointerException.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. Consider the following class.
```java
public class Box {
private int width;
private int height;
public Box(int w, int h) {
width = w;
height = h;
}
}
```
Which statement correctly creates a Box that is 4 wide and 7 tall?
(A) `Box b = new Box(4, 7);`
(B) `Box b = new Box();`
(C) `Box b = Box(4, 7);`
(D) `Box b = new Box(width = 4, height = 7);`
(E) `Box b = new Box(7, 4);`
Show worked answer →
The answer is (A).
The constructor takes two int parameters, w then h, in that order, and assigns them to width and height. So new Box(4, 7) passes 4 to w (width) and 7 to h (height). The new keyword is required to create an object. (B) fails because there is no no-argument constructor. (C) omits new. (D) is not valid Java syntax. (E) swaps the arguments, giving width 7 and height 4.
Markers reward using new with arguments in the parameter order the constructor declares.
AP 2020 (style)4 marksFree response (code writing). Write a class `Point` with two private `int` instance variables `x` and `y`. Provide a constructor that takes two `int` parameters and initialises `x` and `y` from them, and a second no-argument constructor that initialises both to 0.
Show worked answer →
A 4-point question testing two overloaded constructors.
public class Point {
private int x;
private int y;
public Point(int startX, int startY) {
x = startX;
y = startY;
}
public Point() {
x = 0;
y = 0;
}
}
Point 1: both private instance variables declared. Point 2: the two-parameter constructor assigns the parameters to the instance variables. Point 3: a no-argument constructor that sets both to 0. Point 4: both constructors named Point with no return type (overloading by parameter list). Giving either constructor a return type, or reusing the same parameter list, would be an error.
Related dot points
- Topic 5.1 Anatomy of a Class: identify the parts of a class definition (the class header, private instance variables, constructors and methods) and explain encapsulation through access modifiers.
A focused answer to AP CSA Topic 5.1, covering the class header, private instance variables, constructors and methods, the meaning of public and private, encapsulation, and how the pieces combine into a working class, with a fully worked example.
- Topic 5.4 Accessor Methods: write accessor (getter) methods, including a toString method, that return information about an object's state without changing it.
A focused answer to AP CSA Topic 5.4, covering accessor (getter) methods, the non-void return type, why accessors do not change state, the toString method and how println uses it, and returning computed values, with a fully worked example.
- Topic 5.5 Mutator Methods: write mutator (setter) methods, usually void, that change an object's instance variables, including methods that validate or constrain the new value before assigning it.
A focused answer to AP CSA Topic 5.5, covering mutator (setter) methods, the void return type, changing instance variables from a parameter, validating a new value before assigning, and why mutators protect encapsulated data, with a fully worked example.
- Topic 5.9 this Keyword: use the implicit parameter this to refer to the current object, disambiguate an instance variable from a parameter of the same name, and pass the current object to another method.
A focused answer to AP CSA Topic 5.9, covering what this refers to, using this to distinguish an instance variable from a shadowing parameter, calling another method on the current object with this, and passing this as an argument, with a fully worked example.
- Topic 2.2 Creating and Storing Objects (Instantiation): use the new keyword to call a constructor and instantiate an object, choosing the correct constructor by its parameters, and store the result in a reference variable.
A focused answer to AP CSA Topic 2.2, covering the new keyword, constructors and how they initialise an object, choosing a constructor by its parameter list, matching argument types, storing the reference, and the meaning of null, with a worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)