Java Method References
Java 8 introduced lambda expressions to make code shorter and cleaner. But there’s another feature that makes lambda code even more concise — Method References.
If you’ve ever written a lambda that just calls an existing method, method references can make that code more readable.
In this article, we’ll break down:
- What method references are
- The different types of method references
- When to use them
- Real-world examples
1. What is a Method Reference?
A method reference is a shorthand notation of a lambda expression to call an existing method.
Get Prem Osipalli’s stories in your inbox
Join Medium for free to get updates from this writer.
Instead of writing:
list.forEach(s -> System.out.println(s));You can write:
list.forEach(System.out::println);Both do the same thing — print each element — but the method reference is shorter and easier to read.
2. Syntax of Method Reference
The general syntax is:
ClassName::methodNameOr, if you have an object:
instanceReference::methodName3. Types of Method References
There are four types of method references in Java:
3.1 Reference to a Static Method
Format:
ClassName::staticMethodNameExample:
import java.util.Arrays;
public class StaticMethodReferenceExample {
public static void main(String[] args) {
String[] names = {"Bob", "Alice", "Charlie"};
// Using lambda
Arrays.sort(names, (a, b) -> String.compareToIgnoreCase(a, b));
// Using method reference
Arrays.sort(names, String::compareToIgnoreCase);
System.out.println(Arrays.toString(names));
// Output: [Alice, Bob, Charlie]
}
}Here, String::compareToIgnoreCase replaces the lambda (a, b) -> a.compareToIgnoreCase(b).
3.2 Reference to an Instance Method of a Particular Object
Format:
instance::instanceMethodNameExample:
public class InstanceMethodReferenceExample {
public void printMessage(String message) {
System.out.println(message);
}
public static void main(String[] args) {
InstanceMethodReferenceExample printer = new InstanceMethodReferenceExample();
// Lambda
Consumer<String> lambdaPrinter = msg -> printer.printMessage(msg);
// Method reference
Consumer<String> methodRefPrinter = printer::printMessage;
methodRefPrinter.accept("Hello from method reference!");
}
}3.3 Reference to an Instance Method of an Arbitrary Object of a Particular Type
Format:
ClassName::instanceMethodNameExample:
import java.util.Arrays;
public class ArbitraryObjectMethodReferenceExample {
public static void main(String[] args) {
String[] words = {"apple", "banana", "pear", "orange"};
// Lambda
Arrays.sort(words, (a, b) -> a.compareToIgnoreCase(b));
// Method reference
Arrays.sort(words, String::compareToIgnoreCase);
System.out.println(Arrays.toString(words));
}
}Here, Java automatically calls a.compareToIgnoreCase(b) for each pair.
3.4 Reference to a Constructor
Format:
ClassName::newExample:
import java.util.function.Function;
class Person {
String name;
Person(String name) {
this.name = name;
}
}
public class ConstructorReferenceExample {
public static void main(String[] args) {
// Lambda
Function<String, Person> personFactory1 = name -> new Person(name);
// Constructor reference
Function<String, Person> personFactory2 = Person::new;
Person p = personFactory2.apply("John");
System.out.println(p.name); // Output: John
}
}4. Method Reference vs Lambda — When to Use?
- Use method references when your lambda is only calling an existing method.
- Use lambdas when you need to do extra logic before or after calling a method.
Example:
// Method reference
list.forEach(System.out::println);
// Lambda with extra logic
list.forEach(item -> {
if (!item.isEmpty()) {
System.out.println(item);
}
});5. Real-World Usage of Method References
Printing List Elements
List<String> names = List.of("Tom", "Jerry", "Spike");
names.forEach(System.out::println);Mapping with Streams
List<String> numbers = List.of("1", "2", "3");
List<Integer> intList = numbers.stream()
.map(Integer::parseInt)
.toList();
System.out.println(intList); // [1, 2, 3]Collecting Constructor References
List<String> personNames = List.of("Alice", "Bob");
List<Person> people = personNames.stream()
.map(Person::new)
.toList();7. Key Takeaways
- Method references are shortcuts for lambdas that only call a method.
- They make code cleaner and easier to read.
- There are four types: static method, instance method (specific object), instance method (arbitrary object), and constructor.
- Perfect for use in streams and functional interfaces.






