Method references
What you will learn
A method reference uses :: to adapt an existing method or constructor to a functional interface. It is often shorter than a lambda that simply forwards its arguments. The target interface still determines which overload and argument shape are valid.
- Recognize static, bound instance, unbound instance and constructor references.
- Translate a forwarding lambda into an equivalent method reference when appropriate.
- Explain receiver evaluation and checked-exception constraints.
Use a fresh directory per example. Save as Main.java, compile with javac --release 21 Main.java, then run java Main. Only JDK standard-library dependencies are needed.
Refer to a static method and an unbound instance method
import java.util.*;import java.util.function.*;public class Main {
public static void main(String[] args) { Function<String, Integer> parse = Integer::parseInt; ToIntFunction<String> length = String::length; System.out.println(parse.apply("42")); System.out.println(length.applyAsInt("pencil")); }}Expected output:
426Integer.parseInt accepts the supplied text; String.length operates on that supplied String as its receiver.
Rules and mental model
Four common forms are Type::staticMethod, receiver::instanceMethod, Type::instanceMethod (an unbound receiver supplied as the first functional argument) and Type::new. int[]::new is an array-constructor reference accepting an int length. A reference is not a call; the referenced method normally runs when the functional operation is invoked. However, a bound receiver expression is evaluated at reference creation; a null receiver throws immediately. A lambda such as s -> holder.field.trim() can read a field later, unlike a reference that binds the current field value.
The target interface supplies the required parameter and result types, including adaptation by boxing or unboxing where permitted. Java overload resolution may be ambiguous when several target types fit; use a typed intermediate variable or explicit cast to document the intended contract. A void-returning target can discard a result from a compatible method invocation. A checked exception from the referenced method must be compatible with the target interface’s throws clause. If no suitable target exists, an explicit lambda can handle the exception or adapt additional arguments. Do not replace a clear lambda with a reference if it obscures meaningful argument reordering or validation.
Bind an existing receiver
import java.util.*;import java.util.function.*;public class Main {
public static void main(String[] args) { StringBuilder report = new StringBuilder("Stock: "); Consumer<String> append = report::append; append.accept("pen"); append.accept(", book"); Supplier<String> result = report::toString; System.out.println(result.get()); }}Expected output:
Stock: pen, bookThe references bind report. append returns a builder, but the Consumer discards that result.
Reference constructors
import java.util.*;import java.util.function.*;public class Main {
public static void main(String[] args) { Supplier<ArrayList<String>> makeList = ArrayList::new; IntFunction<int[]> makeArray = int[]::new; List<String> a = makeList.get(); a.add("pen"); System.out.println(a); System.out.println(makeList.get().size()); System.out.println(makeArray.apply(3).length); }}Expected output:
[pen]03Each constructor invocation makes a fresh list. The array constructor uses its argument as the array length.
Disambiguate a method reference by assigning a target
import java.util.*;import java.util.function.*;public class Main {
public static void main(String[] args) { IntUnaryOperator number = Math::abs; Function<Double, Double> decimal = Math::abs; System.out.println(number.applyAsInt(-7)); System.out.println(decimal.apply(-2.5)); }}Expected output:
72.5The declared functional types select appropriate abs overloads; the decimal target uses boxing and unboxing.
Common mistakes
String::length needs a String receiver supplied as an argument; text::length already has its receiver. These are different operation shapes. Avoid assuming a bound reference defers receiver evaluation.
Practice
Use method references to create a ToIntFunction<String> for String length and a Function<String,String> for String.strip. Print the length of the stripped text stock and the length of an empty string.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Method references — knowledge check
Apply the chapter contracts and reason about Java 21-compatible examples.
What does String::length require from a ToIntFunction<String> caller?
What is the shape of text::length for non-null String text?
When does `Supplier<Integer> n = text::length` fail if text is null?
What does Supplier<ArrayList<String>> using ArrayList::new do on each get?
An overloaded call with a method reference is ambiguous. What is a useful fix?