Search code examples
javalambdafunctional-interface

Execute functional interface as lambda


I'm not sure if the title really suits what I want to convey. Let me explain it with an example:

public static void test() {
    Consumer<String> c = System.out::println;
    c.accept("hello world");
}

I want to know if there is a more "sophisticated" way of executing a functional interface, something like c("hello world").

Thanks in advance!


Solution

  • No, it's not possible. Java was not originally designed to be a functional language, so the modern functional features of Java unfortunately feel a bit "bolted-on". On the other hand, some of the more modern languages, such as Kotlin (or slightly less modern ones such as Scala), do allow you to call a functional interface (or equivalent in that language) as if it were a simple method.