In Java 8, a new feature called method reference was introduced. Method reference allows you to refer to a method of a functional interface. It provides a compact and simplified form of a lambda expression. Whenever you find yourself using a lambda expression solely to refer to a method, you can replace that lambda expression with a method reference.
Method references in Java 8 provide a concise and readable alternative to lambda expressions for referring to existing methods or constructors. By using method references, you can reduce the verbosity and boilerplate code associated with lambda expressions when you only need to invoke a method.
To summarise, method reference is a powerful feature in Java 8 that offers a more elegant and concise way to refer to methods of functional interfaces, providing a cleaner code structure.
Types of Method References
There are following types of method references in java:
1. Method Reference to a static method.
2. Method Reference to an instance method.
3. Method Reference to a constructor.
1. Method Reference to a static method.
In Java, you can refer to a static method defined within a class using method references. The syntax for referring to a static method is straightforward. Here is an example that demonstrates how to refer to a static method in Java:
Syntax:
javaClassName::staticMethodName
Example:
javaimport java.util.Arrays;
import java.util.List;
public class StaticMethodReferenceExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Jane", "Alice");
names.forEach(StaticMethodReferenceExample::printName);
}
public static void printName(String name) {
System.out.println("Hello, " + name);
}
}
In this example, the static method printName in the StaticMethodReferenceExample class is referred to using the syntax StaticMethodReferenceExample::printName. The method reference is then passed as an argument to the forEach method, which invokes the printName method for each element in the names list.
By using method references, you can simplify your code and improve readability by directly referring to the static method instead of writing a lambda expression to invoke it.
In summary, method references in Java provide a convenient way to refer to static methods within a class. They allow you to streamline your code and make it more concise, enhancing code readability and maintainability.
2) Reference to an Instance Method
In Java, you can refer to instance methods using method references. Similar to static methods, referring to instance methods through method references provides a concise and convenient way to work with functional interfaces. Here is an example that demonstrates how to refer to an instance method in Java:
Syntax:
javaobjectReference::instanceMethodName
Example:
javaimport java.util.Arrays;
import java.util.List;
public class InstanceMethodReferenceExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Jane", "Alice");
InstanceMethodReferenceExample example = new InstanceMethodReferenceExample();
names.forEach(example::printName);
}
public void printName(String name) {
System.out.println("Hello, " + name);
}
}
In this example, we have an instance method called printName in the InstanceMethodReferenceExample class. To refer to this instance method, we create an instance of the class (example) and then use the method reference example::printName. The method reference is then passed as an argument to the forEach method, which invokes the printName method for each element in the names list.
By using instance method references, you can simplify your code and make it more readable by directly referring to the instance method instead of writing a lambda expression to invoke it.
In summary, method references in Java provide a convenient way to refer to instance methods. They allow you to streamline your code and improve readability by directly referencing the instance method instead of using lambda expressions to invoke it.
3) Reference to a Constructor
In Java, you can refer to constructors using method references by using the new keyword. This allows you to create instances of classes using a functional interface. Here is an example that demonstrates how to refer to a constructor using a functional interface:
Syntax:
javaClassName::new
Example:
javaimport java.util.Arrays;
import java.util.List;
public class ConstructorMethodReferenceExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Jane", "Alice");
names.stream()
.map(Greeting::new)
.forEach(Greeting::sayHello);
}
}
class Greeting {
private String name;
public Greeting(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, " + name);
}
}
In this example, we have a class called Greeting with a constructor that takes a String parameter. To refer to this constructor, we use the method reference Greeting::new. The map operation is then used on the stream of names to create instances of the Greeting class using the constructor reference. Finally, the instance method reference Greeting::sayHello is used to invoke the sayHello method on each instance.
By using constructor method references, you can simplify the code for creating instances of classes, especially when working with functional interfaces and streams.
In summary, method references in Java provide a concise way to refer to constructors using the new keyword. They allow you to create instances of classes using functional interfaces, reducing boilerplate code and enhancing readability.
0 Comments