Skip to content
Tutorials Center
My learning

Topics

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.

Java
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

Java The four kinds

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.

Java Method references in streams

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

Java
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::toString is ambiguous for Function<Integer, String> because both the static toString(int) and the instance toString() fit; write a lambda there.
  • Bound receiver is evaluated once, when the reference is created. System.out::println captures the current value of System.out; list.get(0)::foo calls get(0) at creation, not at each invocation. If the receiver expression is null, the creation throws NullPointerException.
  • Unbound references take the receiver from the first parameter of the interface method, so String::compareTo fits Comparator<String> and String::length fits Function<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::format may 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.