Clean • Professional
Method references are a clean and shorter way to write lambda expressions in Java. They were introduced in Java 8 and are mainly used with functional interfaces and Stream API.
In simple words: A method reference is a shortcut to call an existing method using lambda expression style.
A method reference is a way to refer to an existing method without executing it immediately, using the :: operator.
Syntax of Method Reference
ClassName::methodName
👉 This syntax is used to refer to an existing method instead of writing a lambda expression.
Before method references, we used lambda expressions like this:
list.forEach(name -> System.out.println(name));
With method reference, it becomes:
list.forEach(System.out::println);
In Java, method references are divided into 4 main types. They help make lambda expressions shorter and more readable.
This type is used to refer to a static method of a class.
Example
class MathUtil {
static int square(int x) {
return x * x;
}
}
👉 This example defines a static method square() that returns the square of a number.
Using Lambda Expression
Function<Integer, Integer> f = x -> MathUtil.square(x);
👉 Here, a lambda expression is used to call the static method.
Using Method Reference
Function<Integer, Integer> f = MathUtil::square;
👉 This is the method reference version, which is shorter and cleaner.
Function<Integer,Integer>f=MathUtil::square;
👉 Same work, but shorter and cleaner code using method reference.
This type of method reference is used when we call an instance method of an object.
Example
class Printer {
void print(String msg) {
System.out.println(msg);
}
}
👉 This example defines a class Printer with an instance method print() that displays a message.
Using Lambda Expression
Printer p = new Printer();
Consumer<String> c = msg -> p.print(msg);
👉 Here, lambda expression is calling the instance method using object p.
Using Method Reference
Consumer<String> c = p::print;
👉 This is the method reference version, which is shorter and cleaner.
This type of method reference is used when the method belongs to the class of objects inside a collection, and Java decides which object will call the method at runtime.
Example
List<String>names=Arrays.asList("Ram","Amit","Zoya");
👉 This example creates a list of strings.
Using Lambda Expression
names.sort((a,b) ->a.compareTo(b));
👉 Here, lambda expression compares two strings for sorting.
Using Method Reference
names.sort(String::compareTo);
👉 This is the method reference version, where Java internally applies compareTo() on objects.
This type of method reference is used to create objects using a constructor.
Example
class Student {
Student() {
System.out.println("Student Created");
}
}
👉 This example defines a constructor that prints a message when an object is created.
Using Lambda Expression
Supplier<Student> s = () -> new Student();
👉 Here, lambda expression is used to create a new Student object.
Using Method Reference
Supplier<Student> s = Student::new;
👉 This is the constructor reference version, which is shorter and cleaner.
| Feature | Lambda Expression | Method Reference |
|---|---|---|
| Code Length | Slightly longer | Very short and concise |
| Readability | Good, but depends on logic | Excellent and clean |
| Flexibility | High (you can add custom logic) | Limited (only refers existing method) |
| Performance | Same as method reference | Same as lambda |
| Usage Style | Used when logic is needed | Used when only method call is required |
In real Java applications, both lambda expressions and method references are used to process collections in a simple and readable way.
Using Lambda
names.forEach(name -> System.out.println(name));
👉 Here, lambda expression is used to iterate through the list and print each element.
Using Method Reference
names.forEach(System.out::println);
👉 Here, method reference directly calls the println method for each element in the list.
Use method references when:
Method references are not suitable in every situation.
Avoid them when:
Method references in Java are a clean and powerful shortcut for lambda expressions.
They are mainly used when we only need to call an existing method without adding extra logic.