Method References
A method reference is an even more compact alternative to a lambda expression, used when the lambda's entire body would just call an existing method. Instead of writing x -> someMethod(x), you can point directly at the method with the :: operator. Under the hood, a method reference still targets a functional interface — it is just a shorter way to write the same lambda.
The four forms
Form | Syntax | Equivalent lambda |
|---|---|---|
Static method |
|
|
Instance method of a particular object |
|
|
Instance method of an arbitrary object of a type |
|
|
Constructor reference |
|
|
1. Static method reference
Refers to a static method on a class. Used when the lambda would do nothing but forward its arguments to a static method.
Static method reference
// Lambda
Function<String, Integer> parseLambda = s -> Integer.parseInt(s);
// Method reference — equivalent, and shorter
Function<String, Integer> parseRef = Integer::parseInt;
System.out.println(parseRef.apply("42")); // 422. Instance method of a particular object
Refers to an instance method that will be called on one specific, already-existing object — that object is captured, similarly to how a lambda would capture it.
Bound instance method reference
String greeting = "Hello, World!"; // Lambda Supplier<Integer> lengthLambda = () -> greeting.length(); // Method reference — equivalent, and shorter Supplier<Integer> lengthRef = greeting::length; System.out.println(lengthRef.get()); // 13
3. Instance method of an arbitrary object of a type
Refers to an instance method by class name, where the object it is called on becomes the first parameter supplied at call time, rather than being captured ahead of time. This form is common when working with streams.
Unbound instance method reference
// Lambda
Function<String, Integer> lengthLambda = s -> s.length();
// Method reference — equivalent, and shorter
Function<String, Integer> lengthRef = String::length;
System.out.println(lengthRef.apply("Java")); // 4
// A two-argument example: the first argument becomes the receiver
BiPredicate<String, String> startsWithLambda = (s, prefix) -> s.startsWith(prefix);
BiPredicate<String, String> startsWithRef = String::startsWith;
System.out.println(startsWithRef.test("Hello", "He")); // true4. Constructor reference
Refers to a constructor, used as a Supplier or Function that produces new instances.
Constructor reference
// Lambda
Supplier<ArrayList<String>> newListLambda = () -> new ArrayList<>();
// Method reference — equivalent, and shorter
Supplier<ArrayList<String>> newListRef = ArrayList::new;
List<String> list = newListRef.get();
list.add("first item");
// Constructor reference used to convert values
Function<String, StringBuilder> toBuilderRef = StringBuilder::new;
StringBuilder sb = toBuilderRef.apply("start");A method reference is shorthand for a lambda whose only job is to call an existing method.
Four forms: static (
Class::staticMethod), bound instance (obj::method), unbound instance (Class::method), and constructor (Class::new).The unbound instance form takes the receiver object as its first parameter, not as a captured value.
A constructor reference like
ArrayList::newacts as aSupplierorFunctionthat produces new instances.