Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Constructor Reference: Syntax and Use

Understand Java constructor references, how they match functional interfaces, and when to use them in modern Java code.

~6 min readJava
#Java#Constructor References#Method References#Functional Interfaces
Diagram showing a constructor reference pointing from a functional interface to a Java class constructor

A Java constructor reference is a compact form of a lambda expression that creates an instance of a class. It uses the syntax ClassName::new and can be passed wherever a functional interface that matches the constructor's signature is expected. For example, Supplier<ArrayList<String>> can be assigned ArrayList::new instead of writing () -> new ArrayList<>(). This article explains the syntax, the functional interfaces that make it work, practical examples, and edge cases that often trip up developers.

The Constructor Reference Syntax

The core syntax for a constructor reference is ClassName::new. The compiler infers which constructor to use based on the target functional interface's method signature. For a no-argument constructor, the functional interface must have a method that takes no arguments and returns the class type. Supplier<T> fits this pattern exactly:

java
Supplier<StringBuilder> builderSupplier = StringBuilder::new;

This is equivalent to the lambda () -> new StringBuilder(). The constructor reference is not a special runtime object; it is a more concise way to write the lambda. The compiler treats it as a method reference to the constructor, and the bytecode is generated via invokedynamic just like a lambda.

The same syntax works for constructors with parameters, as long as the functional interface's abstract method accepts the same argument types. For instance, Function<String, Integer> expects a method that takes a String and returns an Integer. The Integer class has a constructor that takes a String, so Integer::new is valid:

java
Function<String, Integer> parser = Integer::new; Integer value = parser.apply("42");

This is equivalent to s -> new Integer(s). Note that in modern Java, Integer.valueOf is usually preferred over the constructor, but the constructor reference demonstrates the mechanism.

How Constructor References Work with Functional Interfaces

A constructor reference is valid only when the target functional interface's method signature matches a constructor of the referenced class. The compiler performs the match at compile time. If no constructor matches, the code does not compile.

The functional interface can be a standard one from java.util.function or a custom interface. For example, consider a Person class with a constructor that takes a name and an age:

java
class Person { private final String name; private final int age; Person(String name, int age) { this.name = name; this.age = age; } }

To create a Person from two arguments, use a functional interface with a two-argument method. BiFunction<String, Integer, Person> works because its apply method takes two arguments and returns a result:

java
BiFunction<String, Integer, Person> personFactory = Person::new; Person p = personFactory.apply("Alice", 30);

If you need more than two parameters, the standard java.util.function package does not provide a generic factory interface for that shape, so you must define your own functional interface. This is a common reason to create a custom functional interface.

Practical Examples with Supplier and Function

The most common uses of constructor references are with Supplier and Function. A Supplier<T> is useful when you need a factory that returns a new instance each time. For example, in a stream pipeline, you might need a fresh collection to collect results:

java
List<String> names = Stream.of("Alice", "Bob") .collect(Collectors.toCollection(ArrayList::new));

Here ArrayList::new is a constructor reference that matches the Supplier<C> expected by toCollection. It creates a new ArrayList for each collection operation.

A Function<T, R> is useful when you need to transform an input into a new object. For example, parsing a string to a BigInteger:

java
Function<String, BigInteger> toBigInteger = BigInteger::new; BigInteger result = toBigInteger.apply("12345678901234567890");

The constructor reference works because BigInteger has a constructor that accepts a String. This pattern is common when mapping data from one representation to another.

Constructor References for Arrays and Collections

Constructor references are not limited to regular classes; they can also reference array constructors. The syntax uses the array type followed by ::new, for example int[]::new. This is useful when you need to create an array of a specific length at runtime. The functional interface IntFunction<T> has a method that takes an int and returns T, which matches array creation:

java
IntFunction<int[]> arrayCreator = int[]::new; int[] values = arrayCreator.apply(5); // creates int[5]

This is particularly handy when working with streams and you need to convert a stream to an array. The toArray method on a stream expects an IntFunction<A[]>, so you can write:

java
String[] names = Stream.of("Alice", "Bob").toArray(String[]::new);

The compiler generates code that allocates a new array of the given size. This avoids writing a lambda like size -> new String[size].

For collections, constructor references are often used with Collectors.toCollection, as shown earlier. You can also use them to create a new collection from a stream of elements:

java
Set<String> uniqueNames = Stream.of("Alice", "Bob", "Alice") .collect(Collectors.toCollection(HashSet::new));

This ensures a HashSet is created rather than a default collection type.

Constructor References vs. Lambda Expressions

A constructor reference is essentially a shorthand for a lambda that calls the constructor. The two are interchangeable in most cases. The main difference is readability: ClassName::new is more concise and signals the intent to create a new instance. A lambda like () -> new ClassName() is more explicit but longer.

There is no functional difference in runtime behavior. Both compile to the same invokedynamic instruction and produce an instance of the functional interface. The constructor reference does not create an extra object or add overhead beyond what the lambda would. The choice between them is mainly stylistic and about clarity.

However, there are cases where a lambda is necessary. If the constructor call requires additional logic, such as argument transformation or validation, you cannot use a constructor reference. For example, if you need to trim a string before passing it to the constructor, you must write a lambda:

java
Function<String, Person> personFactory = name -> new Person(name.trim(), 0);

A constructor reference would not allow you to modify the argument. Similarly, if you need to call a static factory method instead of a constructor, use a method reference to that method, not a constructor reference.

Common Mistakes and Edge Cases

One common mistake is using a constructor reference when the functional interface expects a different signature. For instance, Function<Integer, String> expects a method that takes an Integer and returns a String. The String class does not have a constructor that takes an Integer, so String::new would not compile. The compiler error message is often confusing because it lists all constructors that do not match.

Another edge case involves generic classes. If you have a generic class Box<T>, writing Box::new can be awkward or fail to compile when the target type requires a specific type argument. In such cases, use a lambda with an explicit type, for example:

java
class Box<T> { Box() {} } Supplier<Box<String>> boxSupplier = () -> new Box<>();

Constructor references work best with concrete types; for generic types, a lambda often makes the type clearer.

A third edge case is with inner classes. Non-static inner classes have an implicit reference to the enclosing instance. Their constructor is compiled with an extra outer-instance parameter, so a constructor reference must account for that parameter. For example:

java
class Outer { class Inner { Inner(String name) {} } } BiFunction<Outer, String, Outer.Inner> innerFactory = Outer.Inner::new; Outer outer = new Outer(); Outer.Inner inner = innerFactory.apply(outer, "example");

Static nested classes do not have this issue and can be referenced as NestedClass::new.

Runtime Overhead and When It Matters

Constructor references, like lambdas, are implemented using invokedynamic. The first time a constructor reference is used, the JVM resolves the call site and generates a class that implements the target functional interface. Subsequent uses reuse that generated class, so there is no repeated allocation of a new class. The overhead of creating the functional interface instance is minimal and comparable to that of a lambda.

In performance-sensitive code, the main cost is the actual object construction, not the reference itself. The constructor reference adds no extra allocation beyond the new object. However, if you are creating many objects in a hot loop, the cost of the constructor itself dominates. Using a constructor reference instead of a lambda does not change that cost.

A more important consideration is maintainability. Constructor references make code more declarative and reduce boilerplate, which can improve readability. But they can hide which constructor is being called if the class has multiple constructors. In such cases, a lambda with an explicit new call may be clearer. Choose the form that best communicates intent to the next developer.

Compatibility with Java Versions

Constructor references were introduced in Java 8 and are supported in all later versions. They are part of the method reference syntax, which also includes static method references and instance method references. If you are working with code that targets Java 7 or earlier, you cannot use constructor references; you must use anonymous classes or lambdas (if you have a backport). In modern Java, they are a standard tool.

One subtlety is that constructor references work with any functional interface, not just the ones in java.util.function. If you define a custom interface with a single abstract method, you can use a constructor reference as long as the method signature matches. This is useful when you need a factory with a specific semantic name.

For example, you might define a PersonFactory interface:

java
@FunctionalInterface interface PersonFactory { Person create(String name, int age); }

Then you can use Person::new as the implementation:

java
PersonFactory factory = Person::new; Person p = factory.create("Bob", 25);

This keeps the code expressive and ties the factory to the constructor directly.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.