Are you tired of learning about constructors? I’m tired of writing about them! We’re almost done.
If you noticed in the last two examples, we either had the no-argument constructor or an arguments constructor. Can we have a class with both? That’s rhetorical. Yes, when we have both, we’re overloading the constructor. Overloading a constructor is when multiple constructors are declared, each with their own distinct parameters list:
public class Employee { String name; Integer startYear; String department; Boolean isCurrentEmployee; public Employee() { this.startYear = 2023; } public Employee(String initialName) { this.name = initialName; } public Employee(Integer sYear) { this.startYear = sYear; } }
Now we can instantiate our Employee object with any of these calls:
Employee newEmployeeWithoutData = new Employee(); Employee newEmployeeWithName = new Employee('Elaine Benes'); Integer startYear = 2010; Employee newEmployeeWithStartYear = new Employee(startYear);
In the first call, no arguments are passed in. So, the startYear
instance variable is set via the hardcoded value inside the constructor itself, and name
is not set. Why? Because this matching version of the constructor was called:
public Employee() { this.startYear = 2023; }
In the second call, name
is passed in as an argument, so the constructor accepts it as a parameter and sets the name
instance variable. startYear
is not set this time.
In the third call, an Integer is passed in, so the constructor accepts it as a parameter and uses it to set the startYear
instance variable.
A constructor can accept unlimited parameters, and by unlimited, I mean 32. For example, we could set all member variables via the constructor:
public class Employee { String name; Integer startYear; String department; Boolean isCurrentEmployee; public Employee() { this.startYear = System.today().year(); } public Employee(String initialName, Integer sYear, String dept, Boolean isCurrEmp) { this.name = initialName; this.startYear = sYear; this.department = dept; this.isCurrentEmployee = isCurrEmp; } }
Employee jerryEmployee = new Employee('Jerry', 2015, 'Biz-Ops', true); // jerryEmployee now has all its member variables set
When constructors are overloaded, each constructor must have a unique argument list. This would not work because Apex has no way to different these two constructors and would not know which one to call:
public class Employee { String name; Integer startYear; String department; Boolean isCurrentEmployee; public Employee(String empName) { this.name = empName; } public Employee(String dept) { this.department = dept; } }
Something like this WOULD work because the order of the parameters are different:
public class Employee { String name; Integer startYear; String department; Boolean isCurrentEmployee; public Employee(String empName, Integer sYear) { this.name = empName; this.startYear = sYear; } public Employee(Integer sYear, String empName) { this.startYear = sYear; this.name = empName; } }
You’re a certified constructor expert now!