Lesson · Method References
Method references: the lambda you can shorten to a name
13 min readMethod References
When a lambda only passes its arguments to an existing method, Java lets you write that method's name instead. Four forms cover every case, and each one has a lambda it means exactly.
The pattern: a lambda that only delegates
Look at a lambda whose body does nothing but call a method with the same arguments, maybe in a different order. There is no logic to read, only a forwarding. Method references turn that forwarding into its name.
The rule of thumb: if you can say the lambda out loud as 'just call this method', a reference is usually clearer. If the body adds anything (a null check, a default, two calls, arithmetic), keep the lambda.
// the lambda ... names.forEach(n -> System.out.println(n)); // ... and the reference names.forEach(System.out::println); // the lambda ... names.map(n -> n.length()) // ... and the reference names.map(String::length)
The four forms, each with its lambda
Every method reference is one of four shapes. Naming the shape is how you read a reference you did not write.
| Form | Example | Equivalent lambda |
|---|---|---|
| Static method | Integer::parseInt | s -> Integer.parseInt(s) |
| Bound instance method (receiver fixed) | prefix::length | () -> prefix.length() |
| Unbound instance method (receiver is the argument) | String::length | s -> s.length() |
| Constructor | ArrayList::new | () -> new ArrayList<>() |
Reference decoder · name, shape, lambda
four forms cover everythingit means exactly this lambda
sample run
apply("lambda")→6 (receiver = the argument)
Bound vs unbound: where the receiver comes from
The distinction that trips people up: in prefix::length the receiver is chosen when the reference is created, so the reference takes no parameters. In String::length the receiver is the argument, so the reference takes one.
A bound reference captures the receiver like a lambda captures a local: the object must exist at creation time, and the reference keeps using that same object afterwards. This is how this::save binds a callback to the current instance.
String banner = "welcome"; Supplier<Integer> fixed = banner::length; // receiver baked in: banner Function<String, Integer> any = String::length; // receiver = each argument System.out.println(fixed.get()); // 7 System.out.println(any.apply("lambda")); // 6
Constructor references are shape-driven
ClassName::new is not one reference but a family: the compiler picks the constructor whose parameters match the target interface's single method. Supplier<List<String>> needs the zero-argument constructor; Function<Integer, int[]> maps an int to a new array of that size; BiFunction<String, Integer, StringBuilder> can pick the two-argument StringBuilder constructor.
Supplier<List<String>> empty = ArrayList::new; Function<Integer, int[]> array = int[]::new; System.out.println(empty.get().isEmpty()); // true System.out.println(array.apply(4).length); // 4
When a reference hurts or cannot fit
A reference has no room for anything extra, and occasionally the compiler cannot pick between overloads.
- No extra logic. A default value, a negation, or two calls in sequence require a lambda; do not contort the code to reach for a reference.
- Overload ambiguity is real. If several methods match the shape, javac reports ambiguous reference errors; either write the lambda or add a cast to the target type.
- Value clarity beats symbol count. a -> a.getOwner().getName() is a pipeline of its own; keep it as a lambda. The reference shines when the method name alone tells the story.
- Same machinery, same speed. Lambdas and method references compile to the same invokedynamic pipeline, so choose purely for readability.
Read it as a lambda first, then shorten
The reliable way to decode someone else's method reference: write the lambda it must mean, check the parameters line up with the target's single method, and only then trust the shorthand. The four-form table is the decoder ring.
Interview angles
What are the four kinds of method references?+
Static (Integer::parseInt), bound instance (prefix::length, receiver fixed at creation), unbound instance (String::length, receiver comes from the first argument), and constructor (ArrayList::new). The target interface decides which shape fits.
What is the difference between prefix::length and String::length?+
prefix::length binds a specific object, so it takes no arguments (a Supplier). String::length is unbound: it becomes a method that takes the receiver as its first parameter (a Function<String, Integer>).
When does a method reference not compile?+
When its shape cannot match the target interface's single abstract method, or when name resolution is ambiguous among overloads. Adding a cast to the target type settles most ambiguity cases.
Are method references faster than lambdas?+
No. Both compile to a synthetic method plus an invokedynamic call site, so performance is identical. Choose based on which reads better at the call site.
How does ClassName::new decide which constructor to use?+
By the target functional interface's parameter list. Supplier picks the no-arg constructor, Function<Integer, T> picks the int constructor, and so on; if several constructors match the shape, the reference is ambiguous and needs a cast.