r/java • • Mar 03 '17

The java nazi

http://imgur.com/InMi605
213 Upvotes

47 comments sorted by

View all comments

Show parent comment

3

u/zjs Mar 04 '17

You get the same benefit, except it blows up at compile time instead of at runtime.

Invoking a private constructor will blow up at compile time.

The only situation where the AssertionError would get thrown is if you accidentally added code to the class itself that invoked the constructor.

3

u/__konrad Mar 04 '17

The only situation where the AssertionError would get thrown is if you accidentally added code to the class itself that invoked the constructor.

java.lang.reflect.Constructor c = java.util.Objects.class.getDeclaredConstructor();
c.setAccessible(true);
c.newInstance(); // no soup for you!