Java Static Method Reference: Syntax and Usage
Learn how Java static method references use the ClassName::methodName form to replace simple lambdas, how they work with functional interfaces, and when a lambda is clearer.

When you need to pass an existing static method as a callback, Java's method reference syntax lets you do it without writing a lambda that only calls that method. A static method reference uses the ClassName::methodName form and works wherever a functional interface is expected. This article explains the syntax, how it behaves, where it shines, and the tradeoffs you should consider before replacing every lambda with a method reference.
What Is a Static Method Reference?
A static method reference is a shorthand for a lambda expression that simply invokes a static method. Instead of writing (args) -> ClassName.staticMethod(args), you write ClassName::staticMethod. The compiler infers the parameter list from the target functional interface, so the method signature must match the interface's abstract method.
For example, consider a functional interface that takes an integer and returns a string:
@FunctionalInterface
interface IntToString {
String convert(int number);
}You can implement it with a static method reference:
public class Converter {
public static String toHex(int value) {
return Integer.toHexString(value);
}
}
IntToString converter = Converter::toHex;
String result = converter.convert(255); // "ff"The method reference Converter::toHex is equivalent to the lambda value -> Converter.toHex(value). The compiler checks that toHex accepts an int and returns a String, matching IntToString.convert.
Syntax and Basic Example
The general syntax is ClassName::staticMethodName. The method must be static, and its parameter count and return type must align with the functional interface's abstract method. Here is a minimal example using Integer::parseInt with a Function<String, Integer>:
Function<String, Integer> parser = Integer::parseInt;
Integer number = parser.apply("42");The lambda equivalent is s -> Integer.parseInt(s). The method reference is more compact and directly signals that the existing method is the implementation.
You can also use a static method reference with a custom functional interface that has multiple parameters, as long as the method signature matches:
@FunctionalInterface
interface IntCombiner {
int combine(int left, int right);
}
IntCombiner adder = Integer::sum;
int total = adder.combine(10, 20); // 30Here Integer.sum(int, int) matches the interface's combine method.
How It Works with Functional Interfaces
A method reference is not a separate type; it is a compact expression that the compiler translates into an implementation of a functional interface. The target type is determined by the context—assignment, method argument, or return value. This means you cannot use a method reference without a functional interface in scope.
For example, Comparator is a functional interface. You can create a comparator from a static method whose signature matches compare:
Comparator<Integer> intComparator = Integer::compare;
int result = intComparator.compare(10, 20);Integer.compare(int, int) is static, so the class name precedes the :: operator. The compiler resolves the method reference based on the expected functional interface, so the method must be static and accessible from the current context.
Comparing Static Method References with Lambda Expressions
Both method references and lambdas create instances of functional interfaces. The difference is purely syntactic. A method reference is often more readable when the lambda body is just a single method call. For example, using the IntCombiner interface from earlier:
IntCombiner max = Math::max;
IntCombiner maxLambda = (a, b) -> Math.max(a, b);Both create an IntCombiner that returns the larger of two integers. The method reference is shorter, but it is not faster and does not change behavior.
When the lambda body is more complex—multiple statements, local variables, or control flow—a lambda is the only option. Method references cannot contain logic; they only delegate to a single existing method.
The choice between a static method reference and a lambda often comes down to readability. If the method name clearly describes the operation, the reference is more concise. If the method name is obscure or the call needs additional context, a lambda with descriptive parameter names may be clearer.
Common Use Cases in Streams and Collections
Static method references are especially common in stream pipelines where you need to map, filter, or reduce elements. For instance, converting a list of strings to integers using Integer::parseInt:
List<String> numbers = List.of("1", "2", "3");
List<Integer> parsed = numbers.stream()
.map(Integer::parseInt)
.collect(Collectors.toList());The method reference Integer::parseInt matches Function<String, Integer>, so it works directly with Stream.map.
Another common pattern is using a static method as a predicate. Suppose you have a utility class with a static validation method:
public class Validation {
public static boolean isPositive(int value) {
return value > 0;
}
}
List<Integer> values = List.of(-1, 2, -3, 4);
long positiveCount = values.stream().filter(Validation::isPositive).count();Here Validation::isPositive is a Predicate<Integer>.
Static method references also work with reduce and collect when the method signature matches the expected binary operator or collector. For example, summing a stream of integers with Integer::sum:
int sum = values.stream().reduce(0, Integer::sum);This is a static method reference because Integer.sum(int, int) is static.
Limitations and Edge Cases
Static method references have a few constraints worth knowing.
Overloaded methods: If a class has multiple static methods with the same name but different parameter types, the compiler selects the one that matches the functional interface's signature. If the match is ambiguous, you get a compilation error. For example, Integer::parseInt has two overloads—one taking a String, another taking String and int radix. When used with Function<String, Integer>, the compiler picks the single-argument version. If the target interface expects two arguments, the two-argument version is chosen.
Checked exceptions: A static method that throws a checked exception cannot be used directly with a functional interface that does not declare that exception. For example, if a utility method pause() calls Thread.sleep, it throws InterruptedException. You cannot assign it to Runnable because Runnable.run() does not declare any checked exceptions:
public class Utils {
public static void pause() throws InterruptedException {
Thread.sleep(1000);
}
}
Runnable r = Utils::pause; // does not compileYou would need a lambda that catches the exception or a custom functional interface that allows it.
Generic methods: Static generic methods can be referenced, and the compiler usually infers type arguments from the target type. For example:
public class GenericClass {
public static <T> T identity(T value) {
return value;
}
}
Function<String, String> f = GenericClass::identity; // worksThe compiler infers T from the target type.
Performance and Maintainability Considerations
A static method reference does not introduce runtime overhead compared to a lambda that calls the same method. Both compile to similar bytecode, often using invokedynamic to create a functional interface instance. The method reference is not faster; it is a syntactic convenience. Do not choose a method reference for performance reasons.
From a maintainability perspective, method references can improve readability by reducing boilerplate, but they can also obscure what the method actually does if the method name is not descriptive. When the method name is clear and the call is trivial, a method reference is often better. When the logic is more involved or the method name is ambiguous, a lambda with explicit parameter names helps future readers.
Another maintainability concern is that method references are tied to the method signature. If the static method signature changes, the compiler reports a mismatch at the assignment or method invocation. A lambda with explicit parameter names can make the intended conversion more visible, but the compiler catches problems in both forms.
Choosing Between Method Reference and Lambda
Use a static method reference when the method call is the entire operation and the method name clearly expresses the intent. This is common with standard library methods like Integer::parseInt, Math::max, or your own utility methods that have descriptive names.
Use a lambda when you need to:
- Add extra logic before or after the method call.
- Combine multiple method calls.
- Handle exceptions locally.
- Use local variables from the enclosing scope.
- Make the parameter names explicit for readability.
There is no performance difference, so the decision is purely about code clarity and maintainability. In a stream pipeline, a method reference often reads better than a lambda that only delegates. In a complex callback, a lambda gives you more flexibility.
A practical approach is to start with a lambda, then replace it with a method reference if the body is a single method call and the method name is self-explanatory. This keeps the code concise without sacrificing clarity.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.