Java method references, introduced in Java 8, offer a succinct and expressive way to refer to existing methods or constructors using the ::
operator. They serve as a powerful alternative to verbose lambda expressions, helping developers write clearer and more maintainable code in functional programming contexts. This article covers the four types of method references.
Types of Java Method References
Java supports four main types of method references, grouped by the kind of method they refer to:
-
Reference to a Constructor
References a constructor to create new objects or arrays.
Syntax:ClassName::new
Examples:List
people = names.stream().map(Person::new).toList(); Create new instances with constructor reference for each element in the stream.
Additionally, constructor references can be used for arrays:
import java.util.function.IntFunction; IntFunction
arrayCreator = String[]::new; String[] myArray = arrayCreator.apply(5); System.out.println("Array length: " + myArray.length); // Prints 5 This is especially useful in streams to collect into arrays:
String[] namesArray = names.stream().toArray(String[]::new);
-
Reference to a Static Method
This refers to a static method in a class.
Syntax:ClassName::staticMethodName
Example:Arrays.sort(array, Integer::compare);
-
Reference to an Instance Method of a Particular Object (Bound Method Reference)
This is a bound method reference, tied to a specific, existing object instance. The instance is fixed when the reference is created.
Syntax:instance::instanceMethodName
Example:List
names = List.of("Alice", "Bob"); names.forEach(System.out::println); // System.out is a fixed object Here,
System.out::println
is bound to the particularSystem.out
object. -
Reference to an Instance Method of an Arbitrary Object of a Particular Type (Unbound Method Reference)
This is an unbound method reference where the instance is supplied dynamically when the method is called.
Syntax:ClassName::instanceMethodName
Important Rule:
The first parameter of the functional interface method corresponds to the instance on which the referenced instance method will be invoked. That is, the instance to call the method on is passed as the first argument, and any remaining parameters map directly to the method parameters.
Example:List
team = Arrays.asList("Dan", "Josh", "Cora"); team.sort(String::compareToIgnoreCase); In this example, when the comparator functional interface’s
compare
method is called with two arguments(a, b)
, it is equivalent to callinga.compareToIgnoreCase(b)
on the first parameter instance.
Summary
- Java method references simplify code by allowing concise references to methods and constructors.
- The first type—constructor references—express object and array instantiation clearly.
- The second type is referencing static methods.
- The third type—instance method reference of a particular object—is a bound method reference, fixed on a single object instance.
- The fourth type—instance method reference of an arbitrary object of a particular type—is an unbound method reference, where the instance is provided at call time.
- Constructor references are especially handy for arrays like
String[]
. System.out::println
is a classic example of a bound method reference.
Recent Comments