Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 method reference to static void method

Is there a way to refer static method that returns void?

i've tried this

public Function<Runnable, Void> runner = Platform::runLater;

but it will say "bad return type, cannot convert void to java.lang.Void"

like image 721
vach Avatar asked Oct 23 '14 09:10

vach


People also ask

Can we call static method using method reference?

You can refer to a static method defined in the class using method references.

Which is used to get reference to the static method?

References to static methods A static method reference refers to a static method in a specific class. Its syntax is className::staticMethodName , where className identifies the class and staticMethodName identifies the static method. An example is Integer::bitCount .

Can a method be static and void?

static void method is a static method which does not return any thing. Static method can be called by directly by class name. void method is a method which also return nothing. But for calling simple method you have to create a class object and called method by object.


1 Answers

If your method has no return value, don't use the Function interface.

Use Consumer<Runnable> instead.

public Consumer<Runnable> runner = Platform::runLater;

It represents an operation that accepts a single input argument and returns no result.

like image 58
Eran Avatar answered Sep 28 '22 06:09

Eran