Constructor reference – Java 8

/, Java 8/Constructor reference – Java 8

What is constructor reference in java 8?

As you know constructors are kind of special methods, method reference will also applicable to constructors.  Constructor References can be created using the Class Name and the keyword new with the following syntax.

Syntax :

Example :

The constructor reference for all the below lambdas are Student::new and type of the constructor invocation will be decided based on the target type. To understand it better we will see a use case whose goal is to return a Student instance but the String and int type will be supplied as method argument(s). These use cases are not better programming standards but to understand how Constructor reference work.

Example with no arguments:

Example with one argument:

Example with two arguments: