Last active
June 3, 2022 07:35
-
-
Save OMGZui/1e74f3db5cf7b19a258e0a436d49e91d to your computer and use it in GitHub Desktop.
2 ways to implement arguments with default values in Java methods
This file contains 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 class Foo { | |
private final String a; | |
private final Integer b; | |
public Foo(String a, Integer b) { | |
this.a = a; | |
this.b = b; | |
} | |
} | |
public class FooBuilder { | |
private String a = ""; | |
private Integer b = 0; | |
public FooBuilder setA(String a) { | |
this.a = a; | |
return this; | |
} | |
public FooBuilder setB(Integer b) { | |
this.b = b; | |
return this; | |
} | |
public Foo build() { | |
return new Foo(a, b); | |
} | |
} | |
Foo foo = new FooBuilder().setA("a").build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment