What is an AssertionError? In which case should I throw it from my own code?
Of course the "You shall not instantiate an item of this class" statement has been violated, but if this is the logic behind that, then we should all throw
AssertionErrors
everywhere, and that is obviously not what happens.
The code isn't saying the user shouldn't call the zero-args constructor. The assertion is there to say that as far as the programmer is aware, he/she has made it impossible to call the zero-args constructor (in this case by making it private
and not calling it from within Example
's code). And so if a call occurs, that assertion has been violated, and so AssertionError
is appropriate.
The meaning of an AssertionError
is that something happened that the developer thought was impossible to happen.
So if an AssertionError
is ever thrown, it is a clear sign of a programming error.