Created
March 31, 2026 20:15
-
-
Save 7h3kk1d/093ac28dec78db249a0cc65f5bf80d67 to your computer and use it in GitHub Desktop.
Private record constructor motivation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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