On Fri, 10 May 2024 07:23:38 GMT, Per Minborg <[email protected]> wrote:
>> Pavel Rappo has updated the pull request incrementally with one additional
>> commit since the last revision:
>>
>> Fix System.console().readln(null) in jshell
>>
>> Without it, jshell hangs on me. Will think of a test.
>
> src/java.base/share/classes/java/io/IO.java line 47:
>
>> 45:
>> 46: private IO() {
>> 47: throw new Error("no instances");
>
> Is this necessary?
No, it's not necessary.
My guess is that throwing an unconditional error (usually with a snarky
message) from the inside the sole private constructor is a historical idiom to
deter deep reflectors with their `setAccessible` and what not. Maybe that idiom
is outdated with modules and recent efforts on cranking up the
[integrity](https://openjdk.org/jeps/8305968), that I don't know.
FWIW, here's a snippet[^*] from "Item 4: Enforce noninstantiability with a
private constructor" you referred to in your earlier comment:
> A default constructor is generated only if a class contains no explicit
> constructors, so a class can be made noninstantiable by including a private
> constructor:
>
> // Noninstantiable utility class
> public class UtilityClass {
> // Suppress default constructor for noninstantiability
> private UtilityClass() {
> throw new AssertionError();
> }
> ... // Remainder omitted
> }
>
> Because the explicit constructor is private, it is inaccessible outside of
> the class. The `AssertionError` isn’t strictly required, but it provides
> insurance in case the constructor is accidentally invoked from within the
> class. It guarantees the class will never be instantiated under any
> circumstances.
[^*]: I created that snippet from the second edition, whose page 19 is
conveniently publicly available here:
https://www.google.ie/books/edition/Effective_Java/ka2VUBqHiWkC?hl=en&gbpv=1&dq=Item+4:+Enforce+noninstantiability+with+a+private+constructor&pg=PA19&printsec=frontcover
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/19112#discussion_r1596615677