Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Java 8 introduced the concept of method references. We often see them as similar to lambda expressions.

However, method references and lambda expressions are not exactly the same thing. In this article, we’ll show why they are different and what are the risks of using them in the wrong way.

2. Lambdas and Method References Syntax

To start with, let’s see a few examples of lambda expressions:

Runnable r1 = () -> "some string".toUpperCase();
Consumer<String> c1 = x -> x.toUpperCase();

And a few examples of method references:

Function<String, String> f1 = String::toUpperCase;
Runnable r2 = "some string"::toUpperCase;
Runnable r3 = String::new;

Those examples could make us think about method references as a shortened notation for lambdas.

But let’s take a look at the official Oracle documentation. We can find an interesting example there:

(test ? list.replaceAll(String::trim) : list) :: iterator

As we can see, the Java Language Specification allows us to have a different kind of expressions before double colon operator. The part before the :: is called the target reference.

Next, we’ll discuss the process of method reference evaluation.

3. Method Reference Evaluation

What is going to happen when we run the following code?

public static void main(String[] args) {
    Runnable runnable = (f("some") + f("string"))::toUpperCase;
}

private static String f(String string) {
    System.out.println(string);
    return string;
}

We’ve just created a Runnable object. Nothing more, nothing less. However, the output is:

some
string

It happened because the target reference is evaluated when the declaration is first spotted. Hence, we’ve lost the desired laziness. The target reference is also evaluated only once. So if we add this line to the above example:

runnable.run()

We will not see any output. What about the next case?

SomeWorker worker = null;
Runnable workLambda = () -> worker.work() // ok
Runnable workMethodReference = worker::work; // boom! NullPointerException

The explanation provided by the documentation mentioned before:

“A method invocation expression (§15.12) that invokes an instance method throws a NullPointerException if the target reference is null.”

The best way to prevent unexpected situations might be to never use variable access and complex expressions as target references.

A good idea might be to use method references only as a neat, short notation for its lambda equivalent. Having just a class name before :: operator guarantees safety.

4. Conclusion

In this article, we’ve learned about the evaluation process of method references.

We know the risks and the rules we should follow to not be suddenly surprised by the behavior of our application.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW