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.

3

u/pushthestack Mar 03 '17

How do you mean?

11

u/CubsThisYear Mar 03 '17

Since interfaces can have static methods, if you want a collection of static methods (commonly referred to as a static utility class), just put them in an interface instead. Can't instantiate it and that's checked at compile time.

3

u/beeeeeeefcake Mar 04 '17

Scope of constructors is checked at compile time too. And accessing static methods in a non-static way is a warning.

Anyhow I'm having a hard time thinking of what can go wrong instantiating a util class... With a private constructor only code within that class could even do it.