Skip to main content

Java 8 - Constructor References


This is same as the method references, except the method name is new.


When we have multiple constructors which constructor will be selected? It depends on the context.

(firstName) -> new User(firstName) => User::new
(firstName, lastName) -> new User(firstName, lastName) => User::new

This can also be used with arrays with exact one parameter.

n -> new int[n]

This helps to create arrays with generic types.

Object[] array = stream.toArray();
User[] users = stream.toArray(User[]::new);