Java Reference ยท Keywords and Statements
Java method references (::)
Reference for Java method references: the four kinds (static, bound, unbound, constructor), when they replace a lambda and how overloads are resolved.
A method reference is a shorthand for a lambda that does nothing but call an existing method or constructor. It uses the double colon ::. Where a lambda says s -> s.length(), a method reference says String::length. Both need a functional interface as target type, and both compile to the same kind of call site; the reference is simply shorter and names the method directly.
Use a method reference when it reads better than the lambda. When you would have to add arguments or logic, stay with a lambda.
Function<String, Integer> len = String::length; // unbound instance method Supplier<List<String>> make = ArrayList::new; // constructor Function<Integer, int[]> arr = int[]::new; // array constructor
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
ClassName::staticMethod instanceExpression::instanceMethod ClassName::instanceMethod super::method this::method ClassName::new Type[]::new
| Kind | Equivalent lambda | Example |
|---|---|---|
| Static method | (args) -> Class.method(args) |
Integer::parseInt |
| Bound instance method | (args) -> expr.method(args) |
System.out::println |
| Unbound instance method | (obj, args) -> obj.method(args) |
String::length |
| Constructor | (args) -> new Class(args) |
ArrayList::new |
| Array constructor | n -> new Type[n] |
String[]::new |
Usage notes
- Overload resolution: the compiler picks the method by the argument types of the functional interface.
Integer::toStringis ambiguous forFunction<Integer, String>because both the statictoString(int)and the instancetoString()fit; write a lambda there. - Bound receiver is evaluated once, when the reference is created.
System.out::printlncaptures the current value ofSystem.out;list.get(0)::foocallsget(0)at creation, not at each invocation. If the receiver expression isnull, the creation throwsNullPointerException. - Unbound references take the receiver from the first parameter of the interface method, so
String::compareTofitsComparator<String>andString::lengthfitsFunction<String, Integer>. - Generic methods and constructors: you may give explicit type arguments (
this::<String>convert,ArrayList<String>::new), usually the compiler infers them. - Varargs and boxing adaptations are applied as with normal calls. A reference to
String::formatmay still be accepted for several interfaces; check the types. - Not always better.
x -> foo(x, 1)has no reference form. Refactoring to a helper method just to use::is only worth it if the helper is meaningful. - Stack traces and debugging: a method reference stack frame shows up as a generated lambda frame, not a distinct class.
- Checked exceptions follow the same rule as lambdas: the referenced method may only throw what the functional interface's method declares.