Skip to content

Instantly share code, notes, and snippets.

@ahndmal
Created April 30, 2025 08:02
Show Gist options
  • Save ahndmal/a6190683d7f356915651f8038483c963 to your computer and use it in GitHub Desktop.
Save ahndmal/a6190683d7f356915651f8038483c963 to your computer and use it in GitHub Desktop.
public record Point(int x, int y) {
public static final class Builder {
private int x;
private int y;
public Builder(Point point) {
this.x = point.x();
this.y = point.y();
}
public Builder x(int x) {
this.x = x;
return this;
}
public Builder y(int y) {
this.y = y;
return this;
}
public Point build() {
return new Point(this.x, this.y);
}
}
}
var original = new Point(23, 42);
// => Point[x=23, y=42]
var updated = new Point.Builder(original)
.x(5)
.build();
// => Point[x=5, y=42]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment