Ich möchte das, was ich unter 1. mit Lambda geschrieben habe als 2. eine Methodenreferenz verwenden. Leider kompliert dies nicht. Wie schreibt man dies richtig?
Als Compiler-Meldung kommt: Cannot make a static reference to the non-static method repl(Object, Object, Object) from the type Replacer
Als Compiler-Meldung kommt: Cannot make a static reference to the non-static method repl(Object, Object, Object) from the type Replacer
Java:
@FunctionalInterface
interface Replacer<S>{
S repl(S s1, S s2, S s3);
}
public class MethodenReferenz {
public static void main(String[] args) {
//1. mit Lambda
Replacer<String> repl = (s, cs1, cs2) -> s.replace(cs1, cs2);
System.out.println("repl: " + repl.repl("xxxTest", "x", "y"));
//2. mit Methodenreferenz
Replacer<String> repl2 = Replacer::repl;
System.out.println("repl2: " + repl2.repl("xxxTest", "x", "y"));
}
}
Zuletzt bearbeitet von einem Moderator: