Why when using an instance method in method reference (::
) does not throw a NullPointerException
if we set the Object instance to null?
public class MethodReferenceTest {
public static void main(String...strings){
PredicateSample predicateSample = new PredicateSample();
Predicate<String> predicate = predicateSample::isNotEmpty;
// NullpointerException ???
predicateSample = null;
Arrays.asList("a","b","c",null)
.stream()
.filter(predicate)
.forEach(System.out::println);
}
static class PredicateSample{
public boolean isNotEmpty(String s){
return ((s!=null) && (!s.isEmpty()));
}
}
}
It seems like predicateSample
is not invoked after constructing the Predicate?