The use of :: denotes a Method Reference and these can be used in any place where a @FunctionalInterface
is required. Examples are Consumer
s, Function
s, Predicate
s, Supplier
s etc.... A lot of the Java 8 features.
E.g. if a method requires a String which is provided through a Supplier<String>
public int methodUsingSupplier(Supplier<String> supplier) {
String s = supplier.get();
...
}
and there is a method
public static String strProvider() {
return "my String";
}
in "MyClass"
then you can:
int value = methodUsingSupplier(MyClass::strProvider);
as a way of passing the strProvider
method as a 'reference' to the methodUsingSupplier
method which can then utilise it.
As previously said in other answers the compiler ensures that the types and arguments match