Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store a method in a variable in Java 8?

Is it possible to store a method into a variable? Something like

 public void store() {
     SomeClass foo = <getName() method>;
     //...
     String value = foo.call();
 }

 private String getName() {
     return "hello";
 }

I think this is possible with lambdas but I don't know how.

like image 490
fangio Avatar asked Mar 23 '15 20:03

fangio


People also ask

Can we store a method in variable in Java?

You can use Java 8 method references. You can use the :: 'operator' to grab a method reference from an object. You just need a @FunctionalInterface that matches the signature of the method you want to store.

Can you store a method into a variable?

Yeah you can do that.

How do you assign a method in Java?

assign() Method. The Object. assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. Objects are assigned and copied by reference.

How do you pass a method name as a parameter in Java?

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.


3 Answers

Yes, you can have a variable reference to any method. For simple methods it's usually enough to use java.util.function.* classes. Here's a working example:

import java.util.function.Consumer;

public class Main {

    public static void main(String[] args) {
        final Consumer<Integer> simpleReference = Main::someMethod;
        simpleReference.accept(1);

        final Consumer<Integer> another = i -> System.out.println(i);
        another.accept(2);
    }

    private static void someMethod(int value) {
        System.out.println(value);
    }
}

If your method does not match any of those interfaces, you can define your own. The only requirement is that is must have a single abstract method.

public class Main {

    public static void main(String[] args) {
    
        final MyInterface foo = Main::test;
        final String result = foo.someMethod(1, 2, 3);
        System.out.println(result);
    }

    private static String test(int foo, int bar, int baz) {
        return "hello";
    }

    @FunctionalInterface // Not required, but expresses intent that this is designed 
                         // as a lambda target
    public interface MyInterface {
        String someMethod(int foo, int bar, int baz);
    }
}
like image 60
esin88 Avatar answered Oct 17 '22 16:10

esin88


You can use Java 8 method references. You can use the :: 'operator' to grab a method reference from an object.

import java.util.function.IntConsumer;

class Test {
    private int i;
    public Test() { this.i = 0; }
    public void inc(int x) { this.i += x; }
    public int get() { return this.i; }

    public static void main(String[] args) {
        Test t = new Test();
        IntConsumer c = t::inc;
        c.accept(3);
        System.out.println(t.get());
        // prints 3
    }
}

You just need a @FunctionalInterface that matches the signature of the method you want to store. java.util.function contains a selection of the most commonly used ones.

like image 14
axblount Avatar answered Oct 17 '22 17:10

axblount


You can use method reference like -

System.out::println  

Which is equivalent to the lambda expression -

x -> System.out.println(x).  

Moreover you can user reflection to store method and it will works for the earlier version of java too.

like image 1
Razib Avatar answered Oct 17 '22 16:10

Razib