Records feedback

David Aaron Kaye kayedavi at gmail.com
Thu Oct 1 20:09:38 UTC 2020


Good afternoon,

I am very excited to see Records coming to Java. I have been playing around
with them since JDK 14. I noticed that in the proposal, JDK-8246771
<https://bugs.openjdk.java.net/browse/JDK-8246771>, it says

...if the canonical constructor is explicitly declared then its access
modifier must provide at least as much access as the record class


I have implemented a functional List:

public sealed interface List<A> permits Cons, Nil {
}

public record Cons<A>(A head, List<A> tail) implements List<A> {
}

@SuppressWarnings("rawtypes")
public record Nil<Nothing>() implements List<Nothing> {
    public static final Nil instance = new Nil();

    private Nil() {
    }
}

I made the constructor private because I want this to be a singleton,
accessed from instance, but the current proposal does not allow me to do
that, and I get a compilation error on the private constructor. I could
prefer to be able to make the constructor private so that I can have a
record that is a singleton.

Thanks,

David Kaye


More information about the java-se-spec-comments mailing list