Tuesday, August 28, 2018

Java8 - Learn Constructor Reference tutorials with examples

In my previous post, we learned java8 method references with examples.
In this blog post, We are going to learn the Constructor Reference tutorials with examples.

Constructor Reference Introduction

Java8 -  Constructor Reference Arguments Example
Like Method references,  references to Constructor can be created to do object initialization. method references use method name, Constructor references uses new keyword and class name.


ClassName :: new

Constructor Reference Example

 Here is an example for reference of String class name. String constructor is called using the new operator
Supplier<String> stringSupplier = String::new

Constructor reference with arguments example

 First, In below example, Create a Student class with name and id parameters
class Student {
 String name;
 Integer id;

 // Student Contructor
 public Student(String name, Integer id) {
  this.name = name;
  this.id = id;
the second step is to create a Factory Singleton class for Student class.
interface StudentFactory {
 public abstract Student getStudent(String name, Integer id);
StudentFactory is a Functional interface with the single abstract method- getStudent StudentFactory.getStudent() returns Student object.
Next is to create a Student object using constructor reference and supplying two parameters/arguments.
StudentFactory studentFactory = Student::new;
  Student student = studentFactory.getStudent("John", 11);
Constructor reference of Student Object is created using new operator and reference is pointed to StudentFactory. Abstract method return is the same as a constructor of the class getStudent() method of StudentFactory is called with arguments, which internally executes the constructor of Student class.

BiFunction Constructor reference with parameters example

This is another way of referencing constructor with a parameter. BiFunction is a functional interface with single abstract method apply - which accepts two parameters as arguments and produces output as a result.
BiFunction<String,Integer,Student> function = Student::new;
Student student=function.apply("Kiran",12);

Constructor reference array example

as of now we have only seen Constructor references with instance creation, We can also create an array using reference
<A> A[] toArray(IntFunction<A[]> generator)
toArray() is terminal Operation which takes elements from String and converts into an array of elements. This accepts an array of IntFunction. IntFunction is a functional interface with the single abstract method, which takes int values and output results. First Create a User Object with the default constructor
class User {
 private String name;

 public User() {

 public User(String name) {
  this.name = name;

 public String getName() {
  return name;

 public void setName(String name) {
  this.name = name;
Create a List of String names, We mapped this list of string to User Instance using the one-argument constructor with a lambda expression.
List<String> usernames = Arrays.asList("Kiran", "Franc", "Tom", "John");
List<User> users = usernames.stream().map(name -> new User(name)) // lambda expression
We replaced lambda expression with constructor reference as follows using one argument constructor Created Stream for an array of objects, Map method is called on each string of a stream array. Constructor reference - User:: new is created for each string of array, It calls the User constructor with a parameter.
List<User> users1 = usernames.stream().map(User::new) // Constructor reference
Now we will see the constructor reference array example. the stream of strings is passed to map method with constructor reference and finally called toArray() which returns Array of elements from a stream.
User[] users2 = usernames.stream().map(User::new) // constructor reference for User object
 .toArray(User[]::new); // constructor reference for User array object

Related article