Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Method Reference Syntax

Understand Java method references by learning the four forms, their lambda equivalents, and when each form is most useful.

~4 min readJava
#method references#lambda expressions#functional interfaces#Java syntax
Diagram illustrating four types of Java method references: static, instance on object,, instance on arbitrary object, and constructor.

Java method reference syntax uses the :: operator to create a functional interface instance from an existing method. A functional interface is an interface with a single abstract method, such as Function, Consumer, or Predicate. Method references are a more compact alternative to a lambda expression when the lambda body contains only a call to that method. This article explains the four forms of method references, how they correspond to lambda expressions, and where they are best applied.

The Four Forms of Method Reference Syntax

Method references are grouped into four categories based on the kind of method they point to. In the table below, a and b stand for zero or more method parameters; the exact number depends on the referenced method and the target functional interface.

FormSyntaxLambda equivalent
Static methodContainingClass::staticMethod(a, b) -> ContainingClass.staticMethod(a, b)
Instance method of a particular objectcontainingObject::instanceMethod(a, b) -> containingObject.instanceMethod(a, b)
Instance method of an arbitrary object of a particular typeContainingType::instanceMethod(obj, a, b) -> obj.instanceMethod(a, b)
ConstructorClassName::new(a, b) -> new ClassName(a, b)

Every method reference must appear in a context that expects a functional interface, such as a variable declaration, a method argument, or a return value.

Static Method References

A static method reference points to a method declared with the static keyword. The parameters of the functional interface method are passed directly to the static method.

java
Function<String, Integer> stringToInteger = Integer::parseInt; // Equivalent lambda: Function<String, Integer> lambda = value -> Integer.parseInt(value);

Use a static method reference when the method call is exactly what the lambda would do. For example, Objects::nonNull can act as a Predicate in a stream filter.

Instance Method References on a Particular Object

This form is sometimes called a bound instance method reference. It captures an existing object and calls one of its instance methods. The functional interface's parameters become the method's arguments.

java
Consumer<String> printer = System.out::println; // Equivalent lambda: Consumer<String> lambda = value -> System.out.println(value);

A bound reference is useful when the method receiver is fixed. Here, System.out is the receiver, and the string passed through the functional interface is printed by that receiver.

Instance Method References on an Arbitrary Object of a Particular Type

This form is sometimes called an unbound instance method reference. Instead of capturing one object, it uses the first parameter of the functional interface method as the receiver, and any remaining parameters as method arguments.

java
BiPredicate<String, String> equalsIgnoreCase = String::equalsIgnoreCase; // Equivalent lambda: BiPredicate<String, String> lambda = (left, right) -> left.equalsIgnoreCase(right);

A no-argument instance method can also be used this way. The receiver becomes the only input:

java
Function<String, Integer> stringLength = String::length; // Equivalent lambda: Function<String, Integer> lambda = value -> value.length();

This form is especially common with streams, where the element passed through the pipeline is the receiver.

Constructor References

A constructor reference uses ClassName::new and creates a new instance when the functional interface method is called. The target type selects the constructor whose parameters match the functional interface method.

java
Supplier<ArrayList<String>> listFactory = ArrayList::new; // Equivalent lambda: Supplier<ArrayList<String>> lambda = () -> new ArrayList<>();

You can also pass arguments to a constructor through a functional interface that accepts them:

java
Function<String, StringBuilder> builderFactory = StringBuilder::new; // Equivalent lambda: Function<String, StringBuilder> lambda = value -> new StringBuilder(value);

If no matching constructor is available, the compiler reports an error.

Combining Method References in Streams

Method references often appear in stream pipelines. A short, realistic example combines a static reference, an unbound instance reference, and a bound instance reference:

java
List<String> names = Arrays.asList(" alice ", null, "bob", " "); names.stream() .filter(Objects::nonNull) .map(String::trim) .filter(name -> !name.isEmpty()) .forEach(System.out::println);

Objects::nonNull filters out nulls, String::trim removes surrounding spaces from each element, and System.out::println prints each cleaned value. The middle filter remains a lambda because the body is a negated method call, not a direct call.

Common Pitfalls

  • Forgetting the target type. A method reference such as String::length is not a standalone value. It is only valid where a functional interface is expected.
  • Confusing bound and unbound instance references. System.out::println uses a fixed receiver, while String::length uses the functional interface's first argument as the receiver. Mixing these forms up is the most common source of syntax errors.
  • Using method references for logic that is more than one method call. If the lambda body changes arguments, combines results, or contains extra statements, keep the lambda.
  • Expecting every overloaded method to be unambiguous. The compiler chooses an overloaded method or constructor from the target functional interface. If the choices are unclear, a lambda can make the intent easier to read.
  • Ignoring accessibility. The referenced method or constructor must be accessible from the context where the method reference is used.

When to Prefer Method References

Prefer a method reference when the lambda body is exactly one method call and the parameter order matches. It reduces visual noise and makes the intent obvious. Use an explicit lambda when you need control flow, argument transformation, or multiple statements.

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.