JavaMethod References

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

ClassName::staticMethod

(args) -> ClassName.staticMethod(args)

Instance method of a particular object

object::instanceMethod

(args) -> object.instanceMethod(args)

Instance method of an arbitrary object of a type

ClassName::instanceMethod

(obj, args) -> obj.instanceMethod(args)

Constructor reference

ClassName::new

(args) -> new ClassName(args)

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

Java
// 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"));  // 42
2. 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

Java
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

Java
// 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"));  // true
4. Constructor reference

Refers to a constructor, used as a Supplier or Function that produces new instances.

Constructor reference

Java
// 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");
Note
Method references are purely a syntax shorthand — anything written as a method reference could always be rewritten as an equivalent lambda. Reach for a method reference whenever it reads more clearly than the lambda it replaces; if the logic needs more than a direct method call, a lambda (or a full method) is the better fit.
  • 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::new acts as a Supplier or Function that produces new instances.