I'm trying to set up a enum where each Enum value has a custom method to be called. However, it tells me that the method must be static. Is there a way to reference a non-static method?
My code looks like this
public class Foo {
private enum MyEnum {
TGD410(Foo::doAction);
private MyLambda myLambda;
MyEnum(MyLambda myLambda) {
this.myLambda = myLambda;
}
public void execute(String str1, String str2) {
myLambda.apply(str1, str2);
}
}
public void doAction(String str1, String str2) {
}
@FunctionalInterface
public interface MyLambda{
void apply(String str1, String str2);
}
}
Is there some other way to do what I want to do? I think I need to pass in a reference to the Foo object, but not sure how to specify that, since this
refers to the Enum
Update
Updating to clarify that I'm using Springboot. Foo is a bean. The method in questions uses some other injected values, which is why it can't be static.
I'm considering just not using a Lambda and instead putting my method in another POJO (which implements some common interface), which can be instantiated