Skip to content

Instantly share code, notes, and snippets.

@OMGZui
Last active June 3, 2022 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OMGZui/1e74f3db5cf7b19a258e0a436d49e91d to your computer and use it in GitHub Desktop.
Save OMGZui/1e74f3db5cf7b19a258e0a436d49e91d to your computer and use it in GitHub Desktop.
2 ways to implement arguments with default values in Java methods
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