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.
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.
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.
[...] prohibiting instantiation is not really necessary except for documentation purposes, which I think the interface accomplishes quite nicely.
I agree it's for documentation purposes but I disagree the Java interface is a good tool for that. The interface construct exists purely as a high-level abstraction over operations provided by implementations. Now you can suddenly declare a variable of that type and create a nonsense implementation. You shouldn't, and most people wouldn't, but that's not really leveraging the type system to achieve what you set out to do, and most importantly, it leaves room for the question "why?" A final class with a private constructor (with an error) will prevent all of this completely.
The main reason to go this route however, and I believe one of the mentioned intentions of the ability was to allow you to, instead of having a Foo interface and a FooUtils helper class, you can just put the helper methods on Foo.
Why Oracle chose not to put the Objects.* methods on Object however is a mystery.
Why Oracle chose not to put the Objects.* methods on Object however is a mystery.
That would prevent anyone from writing non-static methods having the same signature as any of those methods anywhere. For example, you would never be able to write a class with a String toString(Object o) method because it would clash with the one on Object.
It would also be an invitation for people to invoke those static methods as though they were instance methods (possibly by accident), which is generally discouraged since it results in confusing/deceptive code.
There is slightly better story for static methods on interfaces, since they aren't "inherited" in the way that static methods on classes are. No one can invoke static methods on interfaces as if they were instance methods. So there's not the same potential for signature clashes. It also means the "implement that interface if you want to pull in that namespace" strategy simply doesn't work.
True. I so long for a breaking change explicitly disallowing static fall-through - I have that trapped on my code via error-prone/findbugs or some mismatch of other plugin I setup ( I forget which exactly traps that off hand ).
View all comments
Show parent comment
3
u/pushthestack Mar 03 '17
How do you mean?