Java 8 Feature – Method References
Method References feature is included in Java 8. This feature is used whenever there is a need for calling an existing method using Lambda Expressions. Method References can be used as an alternative approach for calling such methods as this will create more readable code.
There are majorly 4 types of Method References. All kinds with their respective syntax are mentioned below –
Kinds of Method References
:: is an operator to remember
S. No. | Kind | Syntax |
---|---|---|
1 | Reference to a static method | ContainingClass::staticMethodName |
2 | Reference to an instance method of a particular object | containingObject::instanceMethodName |
3 | Reference to an instance method of any arbitrary object of a particular type | ContainingType::methodName |
4 | Reference to a constructor | ClassName::new |
Examples of Method References
Java Code with Output and explanation for each type of Method References are mentioned below –
Reference to a Static method
Below is the Java Code for “Reference to a static method”.
Java Code
package com.thinkconstructive;
import java.util.function.BiFunction;
public class MethodReferenceDemoOne {
public static <T> T concatOperation(T one, T two, BiFunction<T, T, T> concatenateOp){
return concatenateOp.apply(one, two);
}
public static String concatNames(String one, String two)
{
return one + two;
}
public static void main(String args[])
{
MethodReferenceDemoOne methodReferenceDemoOne = new MethodReferenceDemoOne();
// With Lambda Expression
System.out.println(MethodReferenceDemoOne.concatOperation(
"Head","First",(nameOne, nameTwo) -> nameOne + nameTwo
));
//Without Lambda Exp - With Method References
System.out.println(MethodReferenceDemoOne.concatOperation(
"Java","Black", MethodReferenceDemoOne::concatNames
));
}
}
Output
HeadFirst
JavaBlack
Explanation
In the above code MethodReferenceDemoOne class contains a Static method named as concatNames(). This method is called in main() method with the help of Method References technique and required code gets executed.
Method Reference call is MethodReferenceDemoOne::concatNames
Reference to an instance method of a particular object
Below is the Java Code for “Reference to an instance method of a particular object”.
Java Code
package com.thinkconstructive;
import java.util.function.BiFunction;
public class MethodReferenceDemoOne {
public static <T> T concatOperation(T one, T two, BiFunction<T, T, T> concatenateOp){
return concatenateOp.apply(one, two);
}
public String concatNamesForInstance(String one, String two)
{
return one + two;
}
public static void main(String args[])
{
MethodReferenceDemoOne methodReferenceDemoOne = new MethodReferenceDemoOne();
// With Lambda Expression
System.out.println(MethodReferenceDemoOne.concatOperation(
"Head","First",(nameOne, nameTwo) -> nameOne + nameTwo
));
//Without Lambda Exp - With Method References - Instance Method
System.out.println(MethodReferenceDemoOne.concatOperation(
"Oracle", "Java",methodReferenceDemoOne::concatNamesForInstance
));
//Without Lambda Exp - With Method References -
}
}
Output
HeadFirst
OracleJava
Explanation
In the above code MethodReferenceDemoOne class contains an Instance method named as concatNamesForInstance(). This method is called in main() method with the help of Method References technique and required code gets executed.
Method Reference call is methodReferenceDemoOne::concatNamesForInstance
Reference to a Constructor
Below is the Java code for “Reference to Constructor”
Java Code
package com.thinkconstructive;
@FunctionalInterface
interface MRDemoInterface{
MRDemo printMsg(String msg);
}
class MRDemo{
public MRDemo(String msg)
{
System.out.println(msg);
}
}
public class MethodReferenceDemoTwo {
public static void main(String args[])
{
// Reference to a Constructor
MRDemoInterface mrDemoInterface = MRDemo::new;
mrDemoInterface.printMsg("Java Champion");
}
}
Output
Java Champion
Explanation
In the above code MRDemo class contains its Constructor. This class’s Constructor is called in main() method with the help of Method References technique and required code gets executed.
Method Reference call is MRDemo::new
Code Repository
Java Code Example of Method Reference is available HERE
Video Tutorial
Video Tutorial of Method Reference is available HERE