What is method reference ?
To create lambda expressions we use anonymous methods. Some times the implementation that you want to create in lambda is already available in existing method. In those cases method references enable you to refer functional interface method by name in place of lambda expressions.
Our specified method can be either static method or instance method. Functional interface method and our specified method should have same argument and compatible return types, except this remaining things like method name, modifiers are not required to match.
Types of method references :
- Reference to static method.
- Reference to an instance method of a particular object.
- Reference to an instance method of an arbitrary object of a particular type
To define method reference we have to use :: (double colon) operator.
1.Reference to static method
You can refer to static method defined in the class. Following is the syntax and example which describe the process of referring static method in Java.
Syntax:
1 |
<target class name>::<static method name> |
Example with lambda expression :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@FunctionalInterface interface MyFunctionalInterface { boolean isEvenNumber(int i); } public class MethodReferenceDemo { public static void main(String[] args) { //Lambda Example MyFunctionalInterface mi = i -> i%2 == 0; System.out.println(mi.isEvenNumber(15));//false System.out.println(mi.isEvenNumber(20));//true } } |
Same example with method reference :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@FunctionalInterface interface MyFunctionalInterface { boolean isEvenNumber(int i); } public class MethodReferenceDemo { static boolean myImplementation(int i) { return i%2 == 0; } public static void main(String[] args) { //Method reference example MyFunctionalInterface mi = MethodReferenceDemo::myImplementation; System.out.println(mi.isEvenNumber(15));//false System.out.println(mi.isEvenNumber(20));//true } } |
2.Reference to an instance method
Like static methods, you can refer instance methods also. In the following example, we are describing the process of referring the instance method.
Syntax:
1 |
<target class object>::<instance method> |
Reference to instance method example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
@FunctionalInterface interface MyFunctionalInterface { boolean isEvenNumber(int i); } public class MethodReferenceDemo { //instance method boolean myImplementation(int i) { return i%2 == 0; } public static void main(String[] args) { //Method reference example MethodReferenceDemo demo = new MethodReferenceDemo(); MyFunctionalInterface mi = demo::myImplementation; MyFunctionalInterface mi2 = new MethodReferenceDemo()::myImplementation; System.out.println(mi.isEvenNumber(15));//false System.out.println(mi2.isEvenNumber(20));//true } } |
3.Reference to an instance method of an arbitrary object of a particular type
Like static methods, you can refer instance methods also. In the following example, we are describing the process of referring the instance method.
Syntax:
1 |
<arbitary object type>::<instance method> |
This type of method references are little confusing. If you look into the example String::toUpperCase, usually toUpperCase() method is called on a string reference but we have written class name āStringā as like it is a static method. When we use method references they also go through similar checks as lambda expression goes. Compiler will try to match the method reference with any of functional descriptor syntax and if matches then passes on.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@FunctionalInterface interface MyFunctionalInterface { String processString(String name); } public class MethodReferenceDemo { public static void main(String[] args) { MyFunctionalInterface f = String::toUpperCase; System.out.println(f.processString("Peter"));//PETER } } |
Leave A Comment