Most of the IDE’s give you hints, that you could replace lambdas with method references. But there is a subtle difference between them.

I was refactoring commit at work on Vaadin application. To improve readability I started replacing lambdas with method references. After refactoring, and testing I noticed, that button click action resulted in NullPointerException.


In this example, I’ll try to show the difference between lambda expression and method reference.


package com.dovydasvenckus.lambda;

class MyCoolClass {
    public void doSomething() {
        System.out.println("My cool function invoked");

public class Main {

    static MyCoolClass myCoolClass;

    public static void main(String[] args) {
        Runnable runnable = () -> myCoolClass.doSomething();
        myCoolClass = new MyCoolClass();;


My cool function invoked

Method reference

Runnable runnable = myCoolClass::doSomething;
myCoolClass = new MyCoolClass();;


Exception in thread "main" java.lang.NullPointerException
	at java.util.Objects.requireNonNull(
	at com.dovydasvenckus.lambda.Main.main(


An object must be initialized before using method reference operator on it.

Lambdas are bit different, they can access a variable from outside their scope.