Created
April 30, 2025 08:02
-
-
Save ahndmal/a6190683d7f356915651f8038483c963 to your computer and use it in GitHub Desktop.
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 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