Pass a Method as a Parameter in Java

Mohammad Irfan Aug 10, 2021 Jun 03, 2021
  1. Pass a Method as a Parameter by Using the lambda Function in Java
  2. Pass a Method as a Parameter to a Custom Method in Java
  3. Pass a Method as a Parameter Using the Method Reference in Java
Pass a Method as a Parameter in Java

This tutorial introduces the passing of a method as a parameter in Java. To help you understand this topic further, we’ve included example codes.

There’s no concept of a passing method as a parameter in Java from scratch. However, we can achieve this by using the lambda function and method reference in Java 8. So in this article, we’ll focus more on these two topics to pass a method as a parameter.

The lambda function or lambda expression is a concept that was introduced in Java 8. It’s a concise way to write a function by following the functional style approach. Since Java and Java 8 are considered Object-Oriented Languages, they support the functional approach to write the code.

Pass a Method as a Parameter by Using the lambda Function in Java

This is a simple example of lambda, where we are using it to iterate the ArrayList elements. Notice that we’re passing the lambda function to the forEach() method of the Iterable interface. The ArrayList class implements the Iterable interface.

So this is how we can pass a method(lambda function) as a parameter in Java:

public class SimpleTesting{
    public static void main(String[] args) {
        ArrayList<Integer> evens = new ArrayList<Integer>();
        evens.add(10);
        evens.add(20);
        evens.add(30);
        evens.add(40);
        evens.forEach( (n) -> { System.out.println(n); } ); // passing lambda as a parameter
    }
}

Output:

10
20
30
40

Pass a Method as a Parameter to a Custom Method in Java

Apart from the built-in method forEach(), we can pass it as a parameter to a custom method. In this example, we created an interface Doable having a method doSomething(). In the SimpleTesting class, we have a method show() that calls the doSomething() method. Inside the main() method, we created a lambda function and passed it to the show() method.

Notice that this is the line where we are passing a method (lambda function) as a parameter to a method.

show("Hello", doa); // passing lambda function as parameter
interface Doable{
    String doSomething(String str);
}
public class SimpleTesting{
    public static void main(String[] args) {
        Doable doa = (str)-> str+" Rohan";
        show("Hello", doa); // passing lambda function as parameter
    }
    
    public static void show(String msg, Doable doa) {
        String greeting = doa.doSomething(msg);
        System.out.println(greeting);
    }
}

Output:

Hello Rohan

Pass a Method as a Parameter Using the Method Reference in Java

This is another solution that can be used to pass a method as a parameter to a method. It was also introduced with the lambda function in Java 8 version. In this example, we used the method reference concept to pass the show() method as a parameter to the Thread() constructor, which executes during runtime. See the output of the code example here:

public class SimpleTesting{
    public static void main(String[] args) {
        // Passing method reference as a parameter
        Thread thread = new Thread(SimpleTesting::show);
        thread.start();
    }  
    public static void show() {
        System.out.println("My Thread");
    }
}

Output:

My Thread

Related Article - Java Method