Skip to content
Advertisement

Java Consumer MethodReference for nonstatic methods

Code snippet:

JavaScript

}

Which type of functionalInterface can I use instead SomeConsumer? The main issue here is that it is not static field, and the object I will only know in runtime.

I tried to use BiConsumer, however it tells me that I can not refer to non static method in this way.

Advertisement

Answer

From your usage here:

JavaScript

It is quite clear that the consumer consumes 3 things, so it’s not a BiConsumer. You’d need a TriConsumer, which isn’t available in the standard library.

You can write such a functional interface yourself though:

JavaScript

If the only generic parameters that you are ever going to give it is <DocumentPublisher, String, String>, I think you should name it something specific to your application, such as DocumentPublisherAction:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement