Method references is a new Java feature introduced in Java 8. It is used to refer method of functional interface. You can use method references whenever you are using lambda expression to refer to a method. In this case you can replace the lambda expression with a method interface. In this tutorial I will provide some examples of using method references with three different type of method reference:
Lamdbda expressions are used to create anonymous methods. It calls an existing method. However, it is sometimes clearer to refer to the existing method by name and this can be done by using method references. Method references are compact, easy-to-read lambda expressions for methods that already have a name.
- Reference to a static method.
- Reference to an instance method.
- Reference to a constructor.
Reference Using a Static method.
With the help of an interface below we use the static method that can as in the case below with the staticmethodfunction and instantiate it as an object of the interface. We can then use this object of the interface to access our interface method.
//static method
@FunctionalInterface
interface FunctionalStaticInterface{
void interfaceMethod();
}
public class StaticMethodClass {
//my static method
public static void staticMethodFunction(){
System.out.println("calling this static method.");
}
public static void main(String[] args) {
// Referring static method
FunctionalStaticInterface fInterfaceObj =StaticMethodClass::staticMethodFunction;
// Calling interface method
fInterfaceObj.interfaceMethod();
}
}
Reference to an instance method
Below I will give two ways to reference non-static method, 1) using a reference 2) using an anonymous object. When using a non-static method you can first create an object of the main class to later access the method of this class. We reference this method and assign it to an object of the interface which we then use to access to the method of the interface.
@FunctionalInterface
interface MyInstanceInterface{
void interfaceMethod();
}
public class InstanceMethodReference{
public void classMethod(){
System.out.println("My class method");
}
public static void main(String[] args) {
//Referring non-static method using reference
InstanceMethodReference inMeRefObj = new InstanceMethodReference();
System.out.println("using reference");
MyInstanceInterface Myint = inMeRefObj::classMethod;
Myint.interfaceMethod();
}
}
Another way you can reference the method is using an anonymous object. Below we referenced the method in the main class and assigned it to an instantiated object of the Interface.
// Referring non-static method using anonymous object
@FunctionalInterface
interface MyInstanceInterface{
void interfaceMethod();
}
public class InstanceMethodReference{
public void classMethod(){
System.out.println("My class method");
}
public static void main(String[] args) {
System.out.println("using anonymous object");
MyInstanceInterface obj3 = new InstanceMethodReference()::classMethod;
obj3.interfaceMethod();
}
}
Referencing methods using constructors
In the next example we will use the new keyword. Referencing the new constructor with the main class,
//refering a constructor using the new keyword with the help of an interface
@FunctionalInterface
interface ConstructorInterface{
MyConstruct getIt(String m);
}
class MyConstruct{
MyConstruct(String m){
System.out.println("Get "+m);
}
}
public class ReferenceWConstructor{
public static void main(String args[]){
ConstructorInterface ConObj = MyConstruct::new;
ConObj.getIt("It!");
ConObj.getIt("got!");
}
}
The above illustrates a few examples of how you can use method references using interfaces. Keep in mind that java creates an interface for you if the method is predefined, an example being the println method. Below is an example of the print method without the use of an interface.
myArray.stream().forEach(System.out::print);
I hope you enjoyed these examples! Here is the github link to the complete programs to these java examples and feel free to drop any questions on this post below! https://github.com/Nunez350/MethodReference