UNIT 3: CLASS CREATION · TOPIC 3.4
3.4 Constructors
A constructor runs when new is called and sets up the object's initial state. Get the name, the missing return type, and the parameter-to-variable assignments right.
What you need to know
- A constructor has the class's exact name and no return type (not even void).
public Student(String n) { ... } - Its job is to initialize instance variables, usually from parameters. Every instance variable should get a value here, even if it's a default like 0 or an empty list.
- Constructors can be overloaded: a no-argument constructor that sets defaults and a full constructor that takes values.
- If a class has no constructor, Java supplies a default no-argument one. If you write any constructor, that default disappears.
- Parameters that are references (like a String or an array) are copied as references — the object's variable points to the same object the caller passed. For mutable objects this can be a problem; for Strings it's safe because they're immutable.
- A common convention: parameter names differ from instance variable names (
nvsname), or usethis.name = name(3.9).
Worked example
public class Rectangle
{
private double width;
private double height;
public Rectangle() // no-arg: defaults
{
width = 1.0;
height = 1.0;
}
public Rectangle(double w, double h) // full
{
width = w;
height = h;
}
}
Rectangle r1 = new Rectangle(); // 1.0 x 1.0
Rectangle r2 = new Rectangle(3, 4.5); // 3.0 x 4.5
Exam tip: Two mistakes cost FRQ points every year: writing
public void Rectangle(...) (the void makes it a method, not a constructor) and assigning parameters to nothing (w = width; backwards). Instance variable on the left, parameter on the right.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- A constructor's job: put every instance variable in a valid initial state. Usually from parameters, sometimes from defaults (
count = 0,items = new ArrayList<>()). A field left uninitialized gets Java's default, which may not be what you want — an uninitialized ArrayList field is null, and callingaddon it crashes. - Signature rules: name = class name exactly (case matters); no return type at all (not void, not anything); usually public.
public Dog(String n)✓.public void Dog(String n)✗ (a method that happens to be named Dog).public dog(String n)✗ (wrong case, won't compile as a constructor). - Default constructor: if you write zero constructors, Java gives you
public ClassName() {}for free. Write even one constructor and that freebie vanishes —new ClassName()then fails unless you wrote a no-arg constructor yourself. - Overloaded constructors give callers options: no-arg with defaults, partial, full. Arguments select which runs. Within one class, they can't have the same parameter types.
- Parameter naming: either use different names (
n→name) or usethis.name = name(3.9). Using the same name withoutthisassigns the parameter to itself and leaves the field at its default — a silent bug the exam loves. - Reference parameters: if a constructor receives an object (say, an ArrayList) and stores the reference directly, the caller still has a reference to the same list and can modify it behind the object's back. Defensive code copies it. The exam mentions this in 3.6; FRQ 2 rarely requires the copy.
- Constructors can call methods of the class, and can do computation — a
Rectangleconstructor can compute and store the area. But they can't return a value.
Mistakes that cost points
- Adding void. Turns the constructor into a method. The class then has only the default constructor, and
new Dog("Rex")won't compile. - Backwards assignment.
n = name;— the field stays default. Field on the left. - Same-name parameter without this.
name = name;does nothing useful. - Expecting the default constructor to survive. It's gone once you write any constructor.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which of the following is a correctly written constructor for a class named
Timer with a private int seconds instance variable?Show answer
Answer: B. No return type, class name, instance variable assigned from the parameter. A has void (a method), C is backwards, D returns a value.
Q2 A class
Point defines only the constructor public Point(int x, int y). Which of the following will not compile?Show answer
Answer: B. Once any constructor is defined, the default no-arg constructor no longer exists.
Key vocabulary
- Constructor
- a method with the class's name and no return type that initializes a new object
- No-argument constructor
- a constructor with no parameters that sets default values
- Default constructor
- the no-arg constructor Java provides only when a class defines none