Java Lambda – Method Reference And Local Variable

4 min read

Hey Guys!! In this post we will learn about method reference and local variable in context of lambda.

What is Method Reference?

Method reference is a feature which got introduced in Java 8 and its purpose is to simplify implementation of functional interfaces. It can act as shortcut to Lambda expressions.

Variations in syntax

  1. ClassName:: instance-methodname
  2. ClassName::static-methodname
  3. instance::methodname

When to use method reference?

We can use method reference as a short-hand notation if Lambda expression is referring to existing method directly.

Below is an example of using method reference for built in methods.

public class FucntionMethodReferenceExample {

    static Consumer<String> toUppercaseMethodReference = System.out::println; //method reference for sysout

    public static void main(String[] args) {
        toUppercaseMethodReference.accept("Java8");
    }
}

What is constructor reference?

This is feature which got introduced in Java 8 and can be used within lambda expression as a short hand notation for calling constructors with optional parameters. Below is the example.

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student{
    private String name = "myDefaultName";
}

public class ConstructorReferenceExample {
    static Supplier<Student> supplier = Student::new;
    static Function<String, Student> function = Student::new;

    public static void main(String[] args) {
        System.out.println(supplier.get().getName());
        System.out.println(function.apply("Myname").getName());
    }
}

Local Variable in Lambda

A local variable in lambda is a variable declared within the body of the lambda. Below are characteristics of a local variable in lambda.

  1. It cannot have the same name as the lambda p威而鋼 arameter
  2. Cannot be similar to existing local variable names in lambda body.
  3. Cannot re-assign values to these variables.

Following are some common use cases with variables in a lambda.


public class LocalVariablesLambdaExample {
    public static void main(String[] args) {
        int i = 0;

        // 1 -will not compile as variable name and lambda parameter name cannot be same
        Consumer<Integer> consumer = (i) -> System.out.println("Current value :" + i);

        // 2 - will not compile as variable is already assigned with value.
        Consumer<Integer> consumer1 = (i1) -> {
            int i =2;
            System.out.println("Current value :" + i);
        }

        // 3 - will not compile as the value of integer is effectively final, re-assigning local variables is not allowed
        Consumer<Integer> consumer1 = (i1) -> {
            i++;
            System.out.println("Current value :" + i);
        }
    }
}

Though, assigning local variables inside lambda is not allowed, but static or instance variables can be re-assigned within lambda.

What are the advantages of Effectively final?

  1. Easy to perform concurrent operations as it enables immutability.
  2. Paradigm shift to declarative programming.

Hope, this post will be useful in gaining insight in lambda. Keep reading!!

Happy Learning!!