Skip to content

Instantly share code, notes, and snippets.

@7h3kk1d
Created March 31, 2026 20:15
Show Gist options
  • Select an option

  • Save 7h3kk1d/093ac28dec78db249a0cc65f5bf80d67 to your computer and use it in GitHub Desktop.

Select an option

Save 7h3kk1d/093ac28dec78db249a0cc65f5bf80d67 to your computer and use it in GitHub Desktop.
Private record constructor motivation
public record Port(int value) {
// I wish I could make this private
private Port(int value) {
this.value = value;
}
public static Optional<Port> of(int value) {
if (value < 0 || value > 65535) return Optional.empty();
return Optional.of(new Port(value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment