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.
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.
Yeah you can do that.
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.
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.
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);
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With