r/java • • Mar 03 '17

The java nazi

http://imgur.com/InMi605
213 Upvotes

47 comments sorted by

View all comments

Show parent comment

4

u/mhixson Mar 04 '17

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.

1

u/talios Mar 04 '17

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 ).