Java Method Reference and Constructor Reference

These features are introduced in Java 8. These are the alternatives to lambda expression. In Lambda Expression we can write implementation code separately. But in Method and Constructor references can reuse already existing code.

Method Reference:

One method is referring to another method which is already existing with a double colon (::) operator called Method Reference.

class_name::method_name; (use when the method is static)
object_reference::method; (use when the method is non-static i.e., instance method)

Example:

public class Test {

    public static void main(String[] args) {
        //lambda expression
        Runnable runChild = () - > {
            for (int i = 0; i <=8; i++) {
                System.out.println("Child Thread");
            }
        };
        Thread t = new Thread(runChild);
        t.start();
        for (int i = 0; i <=8; i++) {
            System.out.println("Main Thread");
        }
    }

}

Example:

public class Test {
    public static void m1() {
        for (int i = 0; i <=8; i++) {
            System.out.println("Child Thread");
        }
    }

    public static void main(String[] args) {
        //method reference
        Runnable run1 = Test::m1;
        Thread t = new Thread(run1);
        t.start();
        for (int i = 0; i <=8; i++) {
            System.out.println("Main Thread");
        }
    }

}
  • In this example we can reuse code that is an already existing method.
  • Function Interface’s method can be mapped to our specified method with a double colon(::) operator called Method Reference.
  • Specify methods that can take static or instance methods.
  • In Method Reference the argument type must be matched with Functional Interface’s method argument like above “ public void run() ”, “public void m1()”.
  • If the implementation is already available then go for Method Reference.If the implementation is not already available then go for Lambda Expression.

Constructor Reference:

class_name::new;

Example:

class Sample {
    Sample() {
        System.out.println("Sample class Constructor Execution....");
    }
}

interface InterF {
    Sample get();
}

public class Test {

    public static void main(String[] args) {
        //constructor reference
        InterF i = Sample::new;
        Sample s1 = i.get();

    }
}
  • In the above example “InterF’s” get() method refers to “Sample’s” class constructor.

Example:

class Sample {
    Sample(String s) {
        System.out.println("Sample class Constructor Execution with argument:" + s);
    }
}

interface InterF {
    Sample get(String s);
}

public class Test {

    public static void main(String[] args) {
        //constructor reference
        InterF i = Sample::new;
        Sample s1 = i.get("Raj");
        Sample s2 = i.get("Ram");

    }
}
  • If multiple constructors are there then always go for a search-matched argument constructor.

Note: The bigger advantage of these Method and Constructor References is code reusability.

Example:

import java.util.stream.Stream;

public class Test {

  public static void main(String[] args) {
    Stream < String > streamString = Stream.of("Java 8", "Stream API", "with", "Lambda Expression");
    // Call Lambda Expression
    streamString.forEach(s -> System.out.println(s));

  }
}

Output: 

Java 8
Stream API
with
Lambda Expression
  • In this example we are using the Stream API feature with Lambda Expression.

Example:

import java.util.stream.Stream;

public class Test {

  public static void main(String[] args) {
    Stream < String > streamString = Stream.of("Java 8", "Stream API", "with", "Double colon (::) operator");
    // Call double colon operator
    streamString.forEach(System.out::println);
  }
}

Output: 

Java 8
Stream API
with
Double colon (::) operator
  • In this example we are Stream API with a double colon operator feature.
  • In this example reuse code to print the Stream but we are using Lambda expression with the example we created separate code to print the Stream.

Conclusion:

This topic is explained What is Method Reference? What is Constructor Reference? How to write Method and Constructor References? 

Leave a Comment