JAVA 8 Extract predicates as fields or methods?

Viewed 2900

What is the cleaner way of extracting predicates which will have multiple uses. Methods or Class fields?

The two examples:

1.Class Field

void someMethod() {
    IntStream.range(1, 100)
        .filter(isOverFifty)
        .forEach(System.out::println);
}

private IntPredicate isOverFifty = number -> number > 50;

2.Method

void someMethod() {
    IntStream.range(1, 100)
        .filter(isOverFifty())
        .forEach(System.out::println);
} 

private IntPredicate isOverFifty() {
    return number -> number > 50;
}

For me, the field way looks a little bit nicer, but is this the right way? I have my doubts.

3 Answers
Related