Static Method References
You'll learn how to use ClassName::staticMethod syntax as a shorthand for lambda expressions that call static methods.
What Are Static Method References?
When a lambda expression does nothing but call a static method with its parameter(s), you can replace it with a static method reference. This is Java's way of saying "just use that static method directly" without writing the lambda boilerplate.
Instead of writing:
Function<String, Integer> parser = s -> Integer.parseInt(s);
You can write:
Function<String, Integer> parser = Integer::parseInt;
The Syntax
The pattern is simple:
ClassName::staticMethodName
The compiler matches the functional interface's abstract method signature with the static method's signature. The parameters flow through automatically.
Practical Examples
With Consumer:
Consumer<String> printer = System.out::println;
printer.accept("Hello"); // prints: Hello
With BiFunction:
BiFunction<Integer, Integer, Integer> maxFinder = Math::max;
int result = maxFinder.apply(10, 20); // result = 20
With Predicate:
Predicate<String> isEmpty = String::isEmpty; // Wait - this won't work!
// isEmpty is an instance method, not static
The Key Requirement
The static method's parameters must match the functional interface's abstract method parameters. The reference simply "wires" them together without you writing the lambda explicitly.
Key Takeaway: Static method references (ClassName::staticMethod) eliminate lambda boilerplate when your lambda does nothing but forward its arguments to a static method—making your code cleaner and more readable.