Table of contents
1.
Introduction
2.
Why Use Method References?
3.
Key Benefits of Method References in Java 8
4.
Types of Method References
4.1.
Reference to a Static Method
4.2.
Reference to an Instance Method of a Particular Object
4.3.
Reference to an Instance Method of an Arbitrary Object of a Particular Type
4.4.
Reference to a Constructor
5.
Frequently Asked Questions
5.1.
What is a method reference in Java 8?
5.2.
Can we use method references with any method?
5.3.
Can method references be used with overloaded methods?
5.4.
Are method references faster than lambda expressions?
5.5.
Are method references more efficient than lambda expressions?
6.
Conclusion
Last Updated: Jun 9, 2025
Easy

Method Reference in Java 8

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java 8 introduced a variety of features that significantly eased the way developers write code, making it more readable & efficient. Method references, a part of this update, are a notable addition, allowing methods to be used as lambdas. 

Method Reference in Java 8

This article is designed to guide you through the concept of method references, their types, & practical applications in Java 8. By the end of this article, you'll have a firm understanding of this feature & how to implement it in your coding projects.

Let's begin our exploration into the world of Java 8's method references.

Why Use Method References?

Method references in Java 8 offer a concise and readable way to call existing methods, improving code clarity and maintainability. They eliminate the need for verbose lambda expressions when simply calling a method. Common use cases include working with functional interfaces and Java Streams, such as filtering, mapping, or iterating over collections. For example, list.forEach(System.out::println) is cleaner than using a full lambda. By avoiding redundant code, method references help developers write elegant, functional-style Java code with fewer errors and improved maintainability.

Key Benefits of Method References in Java 8

1. Improved Readability
Method references make code shorter and easier to understand by removing unnecessary lambda syntax. For instance, list.forEach(System.out::println) is more readable than list.forEach(x -> System.out.println(x)), especially in stream pipelines where readability matters.

2. Reusability
Existing methods can be reused without rewriting logic. Instead of creating new anonymous functions, you can directly reference class or instance methods, promoting cleaner and DRY (Don’t Repeat Yourself) code.

3. Functional Programming Support
Method references integrate seamlessly with Java 8's functional programming model, especially when working with functional interfaces like Consumer, Function, and Predicate, making code more declarative and expressive.

Types of Method References

Method references in Java 8 are a shorthand notation of a lambda expression to call a method. We'll explore the three main types of method references, providing examples & explanations for each.

  1. Reference to a Static Method
     
  2. Reference to an Instance Method of a Particular Object
     
  3. Reference to an Instance Method of an Arbitrary Object of a Particular Type
     
  4. Reference to a Constructor

Reference to a Static Method

Static method references are used when a lambda expression calls a static method. Here’s the syntax:

ClassName::staticMethodName


Let's consider an example. Suppose we have a utility class with a static method to check if a number is prime:

public class Util {
    public static boolean isPrime(int number) {
        // method logic
    }
}
List<Integer> numbers = Arrays.asList(2, 3, 4, 5, 7, 11, 13);
numbers.stream().filter(Util::isPrime).forEach(System.out::println);


In this example, Util::isPrime is a method reference that points to the static method isPrime in Util class. It’s an alternative to the lambda expression (number) -> Util.isPrime(number).

Reference to an Instance Method of a Particular Object

Instance method references are about methods of a particular object. The syntax looks like:

instance::instanceMethodName


For instance, let's say we have a String list and we want to convert each element to uppercase:

List<String> words = Arrays.asList("Java", "Stream", "Method", "Reference");
words.stream().map(String::toUpperCase).forEach(System.out::println);


Here, String::toUpperCase refers to the toUpperCase method of the String class. This method reference is equivalent to the lambda expression (word) -> word.toUpperCase().

Reference to an Instance Method of an Arbitrary Object of a Particular Type

This reference type is used when each element in a stream is treated as an instance of a particular class, and a method is invoked on each. The syntax is ClassName::instanceMethodName.

List<String> names = Arrays.asList("rahul", "rohit", "virat");
names.stream().map(String::toUpperCase).forEach(System.out::println);

Here, String::toUpperCase applies the method to each String in the list. It’s equivalent to (name) -> name.toUpperCase(). This is useful for applying operations to every object in a collection uniformly.

Reference to a Constructor

Constructor references are used to reference a constructor of a class. The syntax for this is:

ClassName::new
Consider we have a class Person:

public class Person {
    String name;

 Person(String name) {
        this.name = name;
    }
}
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
List<Person> people = names.stream().map(Person::new).collect(Collectors.toList());


In this code snippet, Person::new is a constructor reference which creates a new instance of Person for each element in the names list.

Frequently Asked Questions

What is a method reference in Java 8?

A method reference is a shorthand notation of a lambda expression to call a method.

Can we use method references with any method?

Yes, as long as the method's arguments match the functional interface's abstract method.

Can method references be used with overloaded methods?

Yes, method references can be used with overloaded methods, but Java 8 resolves them based on context from the target functional interface to avoid ambiguity.

Are method references faster than lambda expressions?

Performance is generally similar, but method references may be slightly faster in some cases due to simpler bytecode. However, the difference is usually negligible in Java 8.

Are method references more efficient than lambda expressions?

They are generally more readable & can be more efficient due to improved code reuse.

Conclusion

Method references in Java 8 are a powerful feature that not only simplifies the syntax but also enhances code readability & maintainability. Understanding & implementing these references can greatly improve your coding efficiency & style. As you delve into more complex Java projects, these skills will undoubtedly prove invaluable. Keep practicing & exploring to fully leverage the potential of Java 8!

Live masterclass