Constructor chaining with this(...)
Classes & objects · Intermediate · 14 min · Runs in your Salesforce Developer org · Free
Add three constructors to a `WorkItem` class that chain to each other with `this(...)`. The no-arg and single-arg versions each forward to the fuller one, so only the two-arg constructor writes the fields. Fill in sensible defaults when the caller skips arguments.
Lesson
Why this matters
Most Apex classes let a caller build them in several ways: with everything, with just a name, or with nothing at all. If every constructor copies the same field-assignment code, one tiny fix has to be made three times. Constructor chaining with this(...) keeps the real work in one place and lets the shorter constructors forward to it.
The smallest working example
public class Note {
public String title;
public String colour;
public Note() {
this('Untitled', 'Yellow');
}
public Note(String title) {
this(title, 'Yellow');
}
public Note(String title, String colour) {
this.title = title;
this.colour = colour;
}
}
new Note() ends up calling the two-arg version with 'Untitled' and 'Yellow'. Only one constructor touches the fields.
Name the parts
- **
this(...)** inside a constructor calls another constructor on the same class. - It must be the first statement. You cannot run code or log anything before it.
- The constructor with the most arguments is the one that actually writes
this.subjectandthis.priority. this.x = x;usesthis.so the field (left side) is not confused with the parameter (right side) when they share a name.
A common wrong version
public WorkItem(String subject) {
this.subject = subject;
this('Default', 'Low');
}
Two problems. this(...) is not the first statement, so Apex refuses to compile. And even if it did, the chained call would overwrite the subject you just stored.
The fix
Put this(...) first and let the two-arg constructor do the work:
public WorkItem(String subject) {
this(subject, 'Low');
}
Now you try
Fill in the three constructors on WorkItem. Keep label() as it is; it reads the two fields back out. The harness builds WorkItems with no args, one arg and two args and checks the label.
Learn more
The challenge
Add three constructors to WorkItem so each shorter one delegates to the fuller one.
Examples:
new WorkItem().label()buildsUntitled :: Lownew WorkItem('Email').label()buildsEmail :: Lownew WorkItem('Email', 'High').label()buildsEmail :: High
Requirements:
new WorkItem().label()should beUntitled :: Low.new WorkItem('Email').label()should beEmail :: Low.new WorkItem('Email', 'High').label()should beEmail :: High.new WorkItem('Call back').label()should beCall back :: Low.new WorkItem('Deploy', 'Critical').label()should beDeploy :: Critical.
Starter code
public class WorkItem {
public String subject;
public String priority;
public String label() {
return subject + ' :: ' + priority;
}
}
Related drills
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.
‹ virtual, override and super · finally and several catch blocks ›