50

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.

0

3 Answers 3

55

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);
    }
}
2
  • I get some error how you can only do this in a static context. :/
    – john k
    Mar 3, 2022 at 22:15
  • 1
    If the method you're trying to reference is not static, then you can reference it using "this::test" where the "test" is the function name. Mar 15 at 8:25
14

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.