In this tutorial, you are going to learn ‘how to pass method as a parameter in Java’. and to do that, we are going to use unique concepts introduced in Java 8 and sample codes are provided below to help you understand better.
Before Java 8, there were no such thing as passing method as an argument but Java 8 introduced Lambda Expressions and Method References which can be used to pass as an argument. All thanks to the concept of OOPS and its support of functional style of writing code.
We are going to learn:
- Pass a Method as a Parameter using Lambda Function
- Pass a Method as a Parameter using Consumer Interface
- Pass a Method as a Parameter to another Custom Method using Interface
- Pass a Method as a Parameter using Method Reference
Pass Method as a Parameter in Java 8...!!! Share on X
Pass Method as a Parameter using Lambda Function
In this example, we are iterating an ArrayList and then printing each value of the list. It’s a very simple example where we are passing the Lambda function to the forEach() method. This lambda function is just taking the argument and printing it using sysout.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Arrays; import java.util.List; public class Passing_Method_Using_Lambda{ public static void main(String[] args) { List<String> listOfStudents = Arrays.asList("Sam", "Henry", "Yash", "Yashika", "Donna", "Victoria", "John", "Jenny"); listOfStudents.forEach(x -> System.out.println(x)); //Passing Lambda Function as an argument to another function } } } |
Output:
1 2 3 4 5 6 7 8 | Sam Henry Yash Yashika Donna Victoria John Jenny |
Pass Method as a Parameter using Consumer Interface
In this example, we are iterating an Array List and printing the values which starts with character ‘J’. Java 8 introduced Functional interfaces and in that, one is Consumer Interface. CI takes one input argument and return no result. and the interesting thing is that Lambda expressions can be stored in variable as long as the variable’s type is an interface which has only one method and this perfectly suits to Consumer Interface.
Therefore, in the below example, we are creating a lambda expression and then storing it into a Consumer interface variable and then supplying it to forEach() method. If you are thinking why we are using consumer interface is because the list’s forEach() method takes consumer interface as its parameter. Look at the below example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | interface executeFunc { String execute(String str); } public class Pass_Method_As_Parameter_To_Custom_Method { public static void main(String[] args) { executeFunc exeFunc = (s)-> s +"Uncle Sam"; greeting("Hi, ", exeFunc); } public static void greeting(String str, executeFunc func) { String result = func.execute(str); System.out.println(result); } } |
Output:
1 2 3 4 5 6 7 | Sam start with 'J' : false Henry start with 'J' : false Yash start with 'J' : false Yashika start with 'J' : false Victoria start with 'J' : false John start with 'J' : true Jenny start with 'J' : true |
Pass Method as a Parameter to another Custom Method using Interface
In the above example, we stored the lambda to a built-in consumer interface variable and then passed it to the forEach() method BUT you can create your own interface and store your lambda in that and then you can pass it as a parameter to any custom method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | interface executeFunc { String execute(String str); } public class Pass_Method_As_Parameter_To_Custom_Method { public static void main(String[] args) { executeFunc exeFunc = (s)-> s +"Uncle Sam"; greeting("Hi, ", exeFunc); } public static void greeting(String str, executeFunc func) { String result = func.execute(str); System.out.println(result); } } |
Output:
1 | Hi, Uncle Sam |
Pass Method as a Parameter using Method Reference
Method References came into light with the launch of Java 8 as well just like Lambda. It’s another way of passing a method as argument and considerably helps to shorten the code as well.
Example 1: Using In-Built method
In this example, we are creating Thread object and supplying a reference to the method named ‘runThread’ of class ‘Pass_Method_As_Method_Reference’. So, when we make a Thread.start(), it will execute the runThread() method. and that’s how we pass a method reference to another method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Pass_Method_As_Method_Reference{ public static void main(String[] args) { Thread thread = new Thread(Pass_Method_As_Method_Reference::runThread); // Passing method reference as a parameter thread.start(); } public static void runThread() { System.out.println("It is my Thread"); } } |
Output:
1 | It is my Thread |
Example 2: Using Custom method
Unlike above example, we are not having inbuilt method just like we had ‘Thread’ in the above example, This time, we are creating a custom interface(executeFunc) and adding a method to it (execute). While creating the object of this interface, we are passing a method reference to ‘call’ method which will be executed as soon as we will make the execute() call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | interface executeFunc{ void execute(); } public class Pass_Method_As_Method_Reference { public static void main(String[] args) { executeFunc exeFunc = Pass_Method_As_Method_Reference::call; exeFunc.execute(); } public static void call(){ System.out.println("Hi, Uncle Sam"); } } |
Output:
1 | Hi, Uncle Sam |
This is how, we can pass method as arguments to other methods. These are the simplest examples I have provided here. I hope you learn something very useful today.
Pass Method as a Parameter in Java 8...!!! Share on X
Do you like this Post? – then check my other helpful posts: