Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Lambda Method Reference: When and How to Use

Method references offer a concise way to write lambdas that simply call an existing method. Learn the four method reference types and when to prefer them over lambdas.

~6 min readJava
#Java#Lambda Expressions#Method References#Functional Interfaces
Diagram showing a lambda expression being converted into a method reference arrow in Java code.

When a lambda expression does nothing more than forward its arguments to an existing method, most of the lambda syntax is boilerplate. Java method references let you write that same behavior more concisely. For example, x -> System.out.println(x) becomes System.out::println. This article explains the syntax, the four kinds of method references, and when each one improves your code.

Why Method References Exist

Lambda expressions are the most common way to implement functional interfaces in Java. A functional interface has exactly one abstract method, and a lambda provides its implementation. Consider a simple Consumer<String> that prints a value:

java
Consumer<String> printer = s -> System.out.println(s);

The lambda body is a single call to System.out.println. The parameter s is passed directly to the method. This pattern repeats across many codebases: a lambda that exists only to invoke an existing method. Method references remove the parameter declarations and the arrow, leaving only the method name and its target.

A method reference is not a separate mechanism; it is a more compact form of a lambda expression. The compiler treats it as an implementation of the same functional interface and infers the method signature from that interface, so you do not need to write the parameters explicitly.

The Four Types of Method References

Java supports four kinds of method references. Each corresponds to a different way of invoking a method. The table below summarizes them.

TypeSyntaxLambda Equivalent
Static methodClass::staticMethod(args) -> Class.staticMethod(args)
Instance method of a particular objectobject::instanceMethod(args) -> object.instanceMethod(args)
Instance method of an arbitrary object of a particular typeClass::instanceMethod(obj, args) -> obj.instanceMethod(args)
ConstructorClass::new(args) -> new Class(args)

The first two are straightforward. The third is less obvious: the first parameter of the functional interface becomes the receiver of the method. The fourth uses new as the method name.

Static Method Reference in Practice

A static method reference is useful when you need to pass a method that already exists as a static utility. For example, Integer.parseInt converts a String to an int. With a Function<String, Integer>, you can write:

java
Function<String, Integer> parser = Integer::parseInt;

The lambda equivalent is s -> Integer.parseInt(s). Both work, but the method reference is shorter and directly names the method. This becomes especially readable in streams:

java
List<String> numbers = Arrays.asList("1", "2", "3"); numbers.stream().map(Integer::parseInt).forEach(System.out::println);

The map step uses a static method reference to convert each string. The forEach step uses an instance method reference on a particular object, System.out. Both are idiomatic Java.

Instance Method Reference on a Particular Object

When you have an existing object and want to call one of its instance methods, use object::method. The classic example is System.out::println. Another common case is a logger:

java
Consumer<String> log = logger::info;

This is equivalent to s -> logger.info(s). The method reference captures the logger instance and calls info on it whenever the consumer is invoked. This pattern is useful when you pass a callback that should always target the same object.

Instance Method Reference on an Arbitrary Object

This type is the most confusing for developers new to method references. The syntax looks like a static method reference, but the method is an instance method. The key is that the first parameter of the functional interface becomes the receiver. For example, String::equals can be used as a BiPredicate<String, String>:

java
BiPredicate<String, String> equals = String::equals;

This is equivalent to (s1, s2) -> s1.equals(s2). The first argument to the predicate becomes the object on which equals is called, and the second becomes the argument. This works because equals is an instance method of String. The compiler matches the method signature to the functional interface: the first parameter type must be the receiver type, and the remaining parameters must match the method's arguments.

This form is common in streams when you want to call a method on each element. For example, list.stream().map(String::toUpperCase) uses a method reference to an instance method of an arbitrary object. The map function receives a String and calls toUpperCase on it. The lambda equivalent is s -> s.toUpperCase(). The method reference is more concise and clearly indicates the operation.

Constructor References

Constructor references use Class::new and are useful when you need to supply a factory. For instance, a Supplier<List<String>> that creates a new ArrayList can be written as:

java
Supplier<List<String>> listFactory = ArrayList::new;

The lambda equivalent is () -> new ArrayList<>(). Constructor references become particularly valuable when you pass a factory to a method that needs to create multiple instances. For example, Collectors.toCollection(ArrayList::new) is a common idiom in stream pipelines.

When to Prefer Method References Over Lambdas

Method references are not always better. They are appropriate when the lambda body is a single method call that directly matches the functional interface signature. If the lambda does more than one operation, or if it needs to transform arguments before calling the method, a lambda is clearer. For example, s -> s.trim().toLowerCase() cannot be a method reference because it chains two calls. Similarly, s -> Integer.parseInt(s) * 2 requires a lambda.

Another consideration is readability. A method reference like String::toUpperCase is immediately obvious to most Java developers. However, a method reference to an arbitrary object can be less intuitive if the receiver type is not obvious from context. In that case, a lambda with explicit parameters may be easier to understand. The goal is to reduce cognitive load, not to minimize character count.

Performance and Runtime Behavior

Method references and lambdas are both implemented with invokedynamic and converted to functional interface instances by the JVM. There is no practical performance reason to choose one over the other; the JVM can optimize both forms. The choice is about source code clarity.

A bound method reference such as System.out::println captures the System.out object in the same way the lambda s -> System.out.println(s) does. Any performance difference is unlikely to matter in ordinary code, so choose the clearer form.

Common Pitfalls and Limitations

Method references can fail to compile when the method is overloaded. If a class has multiple methods with the same name but different parameter lists, the compiler may not infer which one the target functional interface requires. In such cases, provide a more explicit target type or fall back to a lambda. For example, Integer::valueOf is overloaded; if the target type does not disambiguate which overload is intended, the compiler reports an error.

Another limitation is generic type inference. When the method is generic, the compiler may not infer the type arguments from the functional interface. This can lead to compilation errors. A lambda or an explicit type witness in the method call often resolves the issue.

Finally, method references to instance methods of an arbitrary object can be confusing when the method has side effects or when the receiver is null. The reference itself does not check for null; the call will throw NullPointerException if the receiver is null. This is the same behavior as a lambda that calls the method on the parameter.

Using Method References in Stream Pipelines

Method references shine in stream pipelines because they reduce boilerplate. Consider a pipeline that reads a list of strings, trims each, converts to uppercase, and collects into a set:

java
Set<String> result = list.stream() .map(String::trim) .map(String::toUpperCase) .collect(Collectors.toSet());

Each map step uses an instance method reference on an arbitrary object. The collect step passes a collector returned by Collectors.toSet(). The pipeline is concise and each step names the operation it performs. The equivalent lambda version would be longer and less direct.

Method references also work well with custom functional interfaces. If you define a functional interface with a single abstract method, you can use a method reference that matches that signature. This keeps your code consistent and avoids anonymous inner classes.

When you are deciding between a lambda and a method reference, ask whether the method reference makes the code easier to read. If the method name and target are clear, use it. If the lambda body contains logic beyond a single call, keep the lambda. Method references are a tool for clarity, not a requirement.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.