The main advantage of the single-element enum approach in this case is that Predicate / Function automatically Serializable .
For this purpose, for example, Guava itself uses enum for some of its Predicate and Function implementations for this reason. Josh Bloch recommends using enum if you need a singleton in Effective Java 2nd Ed., Point 3 . Quote:
This approach is functionally equivalent to the open-source field approach, except that it is more concise, provides free serialization techniques and provides an excellent guarantee against multiple instances even in the face of complex serialization or reflection attacks. Although this approach has not yet been adopted, a singleton enum type is the best way to implement singleton code.
Guava provides these singletones through static methods in its API, though avoiding the ugliness of SomePredicate.INSTANCE in user code.
source share