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.
OK, but under what circumstances would that be an error? Honestly the whole practice of prohibiting instantiation is not really necessary except for documentation purposes, which I think the interface accomplishes quite nicely.
You have to work within the limits of your tools. The fact that Java doesn't support free functions means your going to have some awkwardness somewhere.
In my opinion it's reasonable for someone to accidentally instantiate a static utility class and it's easy to prevent at compile time, so you might as well do it. The anonymous sub class is so esoteric that you'd pretty have to be doing it to be annoying. On top of that, there isn't a good way around it and it doesn't cause any actual problems, so I'm not going to worry about it. Honestly making the code blow up at runtime seems worse than doing nothing at all.
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.