Java8 - Learn Method Reference Tutorials with examples

In this tutorial, the About to learn method reference is introduced in the java8 version.

What is Method reference in java8

Method reference allows the developer to call a method or constructor by their names.

It is one more short way of writing lambda expressions. It uses the symbol :: to separate class/object/constructor from the method name.

Sometimes, we used to get the compile-time warning like “The target type of this expression must be a functional interface” so this error is because of the need to assign method reference or lambda expression to a functional interface.

Here is a method reference syntax.

Classname:: methodname
Instancename:: methodname
Constructorname::new

Various types of method references are introduced.

  • Static methods reference
  • Instance method reference
  • Constructor reference

Static method reference example java8

Static methods are global to class, They can directly call using Classname.staticmethod() with java8, For example, You can call using Classname::static-method.

public class MethodReferenceExample {

    public static void main(String[] args) {
        // Using lambda expression
        BooleanTest lambdaExample = (n) -> PositiveNumberCheck.isPositive(n);
        System.out.println(lambdaExample.test(2));
        System.out.println(lambdaExample.test(-2));

        // Using method reference
        BooleanTest methodRefExample = PositiveNumberCheck::isPositive;
        System.out.println(methodRefExample.test(2));
        System.out.println(methodRefExample.test(-2));
    }
}

@FunctionalInterface
interface BooleanTest {
    boolean test(int n);
}
class PositiveNumberCheck {
    public static boolean isPositive(int n) {
        return n >= 0;
    }
}

And the output of the above code execution.

true
false
true
false

Instance method reference example in java8

Instance methods work on instances of a class. Before java 8, we used to call instances method using the new class().method name with java8. For example, You can call object:: instance-method

import java.util.function.Function;
public class MethodReferenceExample {
    public Integer IncrementByOne(Integer value) {
        return value + 1;
    }
    public static void main(String[] args) {
        Function<Integer, Integer> function = new MethodReferenceExample()::IncrementByOne;
        System.out.println(function.apply(5));
    }
}

output:

6

java8 Construction reference example

Construction reference can be called using the new operator.

Reference to construction is executed via the functional interface. Here is an example for construction reference

import java.util.function.Consumer;
public class MethodReferenceExample {

    public static void main(String[] args) {
        Consumer<String> function = Emp::new;
        function.accept("Hello");
    }
}
class Emp {
    Emp(String msg) {
        System.out.print(msg);
    }
}

Output:

Hello

Conclusion

Learned about method references with static constructor and instance syntax with example