As the other answers have stated, overloading is probably your best option. But just keep in mind that using the Builder Pattern works too for this problem:
class Foo {
private final String sample;
private final ObjectMapper objectMapper = new ObjectMapper();
private Foo(String sample, ObjectMapper objectMapper) {
this.sample = sample;
if (objectMapper != null) {
this.objectMapper = objectMapper;
}
}
public static class Builder {
private final String sample;
private final ObjectMapper objectMapper;
public Builder sample(String sample) {
this.sample = sample;
return this;
}
public Builder objectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}
public Foo build() {
return new Foo(sample, objectMapper);
}
}
}
Then later on:
Foo.Builder
.sample(mySample)
.objectMapper(myObjectMapper)
.build()
Or simply:
Foo.Builder
.sample(mySample)
.build()
I like the elegance and the fluent clarity of this approach. After all, many software experts like Uncle Bob warn about the complexity each additional parameter brings to a method, but this is probably overkill for your simple situation. Still, it is a nice pattern to keep in mind in general.