On Tue, 7 May 2024 02:54:39 GMT, Joe Darcy <[email protected]> wrote:
>> src/java.base/share/classes/java/io/IO.java line 41:
>>
>>> 39: */
>>> 40: @PreviewFeature(feature = PreviewFeature.Feature.IMPLICIT_CLASSES)
>>> 41: public class IO {
>>
>> Should this be final?
>
>> Should this be final?
>
> With only the private constructor, it doesn't matter too much in practice,
> but an explicit `final` would be good documentation if that is the intent.
If the sole constructor of a class is private, not only does it preclude the
class from being instantiated, but it also precludes the class from being
extended. Mind you, even with the sole private constructor, both instantiation
and extension are still possible from inside the class or its nested classes.
As long as we don't leak such instances to API clients, they won't seem to be
able to observe any difference between "the private constructor" and "the
private constructor of a final class".
I think that just having that constructor private but the class "non-final"
makes bigger bang for the buck. We can defer the decision to make the class
final.
If this helps, here are a few well-known similar classes:
- java.util.Collections
- java.util.concurrent.Executors
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/19112#discussion_r1593033564