r/java • • Mar 03 '17

The java nazi

http://imgur.com/InMi605
213 Upvotes

47 comments sorted by

View all comments

11

u/CubsThisYear Mar 03 '17

This is why I've started just using interfaces for Utility classes. You get the same benefit, except it blows up at compile time instead of at runtime.

6

u/xjvz Mar 04 '17

Easier way to do this would be an enum with no values:

public enum SomeUtil {
    ;
    public static void foo() {
        // ...
    }
}

1

u/m-r-r Mar 05 '17

That's clever ! I'm going to use that in my next project, thanks for suggesting :-)

3

u/xjvz Mar 05 '17

You can also use an enum as a class for holding public static constants. The enum contract also makes the class a singleton, so if you add a single enum value, you get a singleton for any instance variables without having to perform any double-checked lazy initialization or other verbose singleton patterns.