Method Reference in Java 8 allows a functional interface method to be mapped to a specific method using the ::
(double colon) operator. This technique simplifies the implementation of functional interfaces by directly referencing existing methods. The referenced method can be either a static method or an instance method. It’s important that the functional interface method and the specified method have matching argument types, while other elements such as return type, method name, and modifiers can differ.
If the specified method is a static method, the syntax is:
ClassName::methodName
If the method is an instance method, the syntax is:
ObjectReference::methodName
A functional interface can refer to a lambda expression and can also refer to a method reference. Therefore, a lambda expression can be replaced with a method reference, making method references an alternative syntax to lambda expressions.
Example with Lambda Expression
class Task {
public static void main(String[] args) {
Runnable r = () -> {
for (int i = 0; i <= 10; i++) {
System.out.println("Child Thread");
}
};
Thread t = new Thread(r);
t.start();
for (int i = 0; i <= 10; i++) {
System.out.println("Main Thread");
}
}
}
Example with Method Reference
class Task {
public static void printChildThread() {
for (int i = 0; i <= 10; i++) {
System.out.println("Child Thread");
}
}
public static void main(String[] args) {
Runnable r = Task::printChildThread;
Thread t = new Thread(r);
t.start();
for (int i = 0; i <= 10; i++) {
System.out.println("Main Thread");
}
}
}
In the above example, the Runnable
interface’s run()
method is referring to the Task
class’s static method printChildThread()
.
Method Reference to an Instance Method
interface Processor {
void process(int i);
}
class Worker {
public void display(int i) {
System.out.println("From Method Reference: " + i);
}
public static void main(String[] args) {
Processor p = i -> System.out.println("From Lambda Expression: " + i);
p.process(10);
Worker worker = new Worker();
Processor p1 = worker::display;
p1.process(20);
}
}
In this example, the functional interface method process()
is referring to the Worker
class instance method display()
.
The main advantage of method references is that we can reuse existing code to implement functional interfaces, enhancing code reusability.
Constructor Reference in Java 8
We can use the ::
(double colon) operator to refer to constructors as well.
Syntax:
ClassName::new
Example:
class Product {
private String name;
Product(String name) {
this.name = name;
System.out.println("Constructor Executed: " + name);
}
}
interface Creator {
Product create(String name);
}
class Factory {
public static void main(String[] args) {
Creator c = name -> new Product(name);
c.create("From Lambda Expression");
Creator c1 = Product::new;
c1.create("From Constructor Reference");
}
}
In this example, the functional interface Creator
is referring to the Product
class constructor.
Note: In method and constructor references, the argument types must match.