Constructor References Java 8 Simplified Tutorial with examples
This tutorial explains the new Java 8 feature known as constructor reference. It starts off with explaining what is a constructor reference by showing its structure and an example. Next, the tutorial shows an example scenario where constructor references can be applied for instantiating objects from an object factory.
(Note - If you are new to the concept of method references then I would recommend that first refer the method references tutorialRead Method References Tutorial.)
What are Constructor References: Constructor Introduced in Java 8, constructor references are specialized form of method references which refer to the constructors of a class. Constructor References can be created using the Class Name and the keyword new with the following syntax -
Syntax of Constructor References:
<ClassName>::new
Constructor Reference Example: If you want reference to the constructor of wrapper class Integer
, then you can write something like this -
Supplier<Integer> integerSupplier = Integer::new
Example usage of Constructor References in code
STEP 1 - Let us define an Employee
class with a constructor having 2 parameters as shown below -
[su_box title="Employee.java" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java]public class Employee{
String name;
Integer age;
//Contructor of employee
public Employee(String name, Integer age){
this.name=name;
this.age=age;
}
}[/java][/su_box]
STEP 2 - Now lets create a factory interface for employees called EmployeeFactory
. getEmployee()
method of EmployeeFactory
will return an employee instance as per the normal design of factory pattern
Read tutorial on Factory Design Pattern.
[su_box title="Interface EmployeeFactory.java" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java]public Interface EmployeeFactory{
public abstract Employee getEmployee(String name, Integer age);
}[/java][/su_box]
Note - Since EmployeeFactory
interface has a single abstract method hence it is a Functional InterfaceRead Tutorial on Functional Interfaces.
STEP 3 - The client side code to invoke EmployeeFactory
to create Employee
instances would be as follows -
[su_box title="Client-side code for invoking Factory interface" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java]EmployeeFactory empFactory=Employee::new;
Employee emp= empFactory.getEmployee("John Hammond", 25);[/java][/su_box]
Explanation of the code
- The Constructor Reference of
Employee
is assigned to an instance ofEmployeeFactory
calledempFactory
. This is possible because the function descriptorRead tutorial on Java 8 Function Descriptors ofEmployee
constructor is same as that of the abstract method of the Functional InterfaceEmployeeFactory
i.e.(String, Integer) ->Employee.
- Then the
getEmployee
method ofempFactory
is called with John's name and age which internally calls the constructor ofEmployee
and a newEmployee
instanceemp
is created.