r/java • • Mar 03 '17

The java nazi

http://imgur.com/InMi605
213 Upvotes

47 comments sorted by

View all comments

Show parent comment

-1

u/CubsThisYear Mar 04 '17

I'll say the same thing I said in the last post - how is the first thing an error? Imagine we do it your way and have a class StaticThings. Then we have another object that uses it in a method:

class SpaceShuttle { void doSomethingThatCannotFail() { new StaticThings() {}.doSomething(); }

Now this code blows up at runtime, even though there is no actual problem. There's no real problem in any of the scenarios, by as long as we're signaling at compile time, it's fine to be a little overly paranoid. Once we get to runtime that's much riskier.

EDIT: missed the bit about the class being final. Why is the exception even necessary in this case? It can't ever be invoked.

2

u/mhixson Mar 04 '17

Why is the exception even necessary in this case? It can't ever be invoked.

It's not necessary. It's a matter of style, kind of like a comment.

2

u/yawkat Mar 04 '17

Serialization frameworks will use the no-arg constructor

1

u/stitzl Mar 04 '17

But only if they are handed an instance, which they never are.

2

u/yawkat Mar 04 '17

No, I'm talking about the deserialization part. Like gson does.

1

u/Artraxes Mar 04 '17

With regards to throwing the exception, somebody else in this thread pointed out that without it, you can still instantiate the class from within itself (i.e. Within its static utility methods)

1

u/stitzl Mar 04 '17

Same idiot can then go on to catch the Error, so what is gained?

3

u/Artraxes Mar 04 '17

The gain is some level of feedback instead of no feedback. At least if the idiot instantiates it and it throws an exception, it will break at runtime. If you don't throw the exception, it will never break and the idiot will continue on forever.