Lambda: A constructor reference produces a Supplier or Function depending on arity

Type::new adapts to whichever functional interface it targets: a no-argument constructor fits a Supplier, while a two-argument constructor fits a BiFunction, all from the same ClassName::new syntax.

Code
class Point {
    final int x, y;
    Point() { this(0, 0); }
    Point(int x, int y) { this.x = x; this.y = y; }
    public String toString() { return "(" + x + "," + y + ")"; }
}
Supplier<Point> origin = Point::new;
BiFunction<Integer, Integer, Point> makePoint = Point::new;
System.out.println("origin: " + origin.get());
System.out.println("makePoint(2,3): " + makePoint.apply(2, 3));
Output
origin: (0,0)
makePoint(2,3): (2,3)
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms