title: Java Records and Null Object Pattern?
body: <p>Is there any way to do Null Objects with Java Records? With classes I'd do it like that:</p>
  <pre><code>public class Id {
  
    public static final Id NULL_ID = new Id();
  
    private String id;
  
    public Id(String id) {
      this.id = Objects.requireNonNull(id);
    }
  
    private Id() {}
  }
  </code></pre>
  <p>But that does not work, because every constructor needs go through the canonical (<code>Id(String id</code>) one and I can't just call <code>super()</code> to go around the invariants.</p>
  <pre><code>public record Id(String id) {
    public static final Id NULL_ID = null; // how?
  
    public Id {
      Objects.requireNonNull(id);
      // ...
    }
  }
  </code></pre>
  <p>Right now I work around this with</p>
  <pre><code>public Id {
    if (NULL_OBJECT != null)
      Objects.requireNonNull(id);
  }
  </code></pre>
  <p>but that feels wrong and open to concurrency problems.</p>
  <p>I haven't found a lot of discussion about the design ideas behind records and this may have been already discussed. If it's like that to keep it simple that's understandable, but it feels awkward and I've hit that problem multiple times already in small samples.</p>