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 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.

Output:

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:

Output:

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. 

Output:

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.

Output:

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. 

Output:

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:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak