Quick revision
The short answer
For a configuration object with 15 fields, four of them required, I would choose a builder that produces an immutable object, often a record, and that checks the four required fields when build() is called or takes them in the builder's constructor. Telescoping constructors become unreadable, a plain record forces every caller to pass all 15 values, and with-style copy methods are a good addition for making variations, not a replacement for the first construction.
What to remember
In this answer, we compared four ways to construct a configuration with 15 fields. Telescoping constructors become unreadable and let same-typed values be swapped. A record with a compact constructor validates well but forces every caller to pass all values. With methods make variations easy but need a base object, and derived record creation is still not final in JDK 25. A builder gives named calls, defaults in one place and validation in build(), and with required fields in its constructor or a step builder it is the choice for this case.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For a configuration object with 15 fields, four of them required, I would choose a builder that produces an immutable object, often a record, and that checks the four required fields when build() is called or takes them in the builder's constructor. Telescoping constructors become unreadable, a plain record forces every caller to pass all 15 values, and with-style copy methods are a good addition for making variations, not a replacement for the first construction.
Why this question is asked
Configuration objects are everywhere in real services: client settings, retry policies, export options, connection pools. They tend to start with three fields and grow to fifteen, and the way we construct them decides whether calls stay readable and whether a wrong value is caught early or in production. Interviewers want to hear the cost of each option and a reasoned choice. In this answer, we will recap the problem with many parameters, look at telescoping constructors, a builder, a record with a compact constructor and with-style copy methods in turn, mention the step builder, and then make the choice for this case.
1A Quick Recap of the Problem
Our object has 15 fields. Four are required, say the host, the port, the user name and the timeout, and eleven are optional with sensible defaults, such as a pool size of 10 or compression switched off. We want three things from the way we construct it: calls that a reader understands without opening the class, defaults that live in one place, and validation that rejects a bad combination before the object is ever used. We also want the finished object to be immutable, so it can be shared between threads without locks.
2Telescoping Constructors and Why They Break Down
The oldest approach is a chain of constructors, each adding one more parameter and passing defaults to the next one. It is called telescoping because the constructors extend like a telescope.
Notice the call site. Several parameters have the same type, so swapping the retry count and the pool size compiles without a warning and fails only at run time, if at all. With eleven optional fields we cannot write a constructor for every combination either: someone who wants the default pool size but a custom retry count still has to pass the pool size. So telescoping constructors are fine for two or three parameters and error-prone beyond that.
3A Builder With Defaults and Validation
A builder is a small helper object that collects values through named methods and then creates the real object in one step. The required fields can go into the builder's constructor, so they cannot be forgotten, and the optional fields start at their defaults.
The call site now reads like a sentence, every optional value is named, and the defaults are written once. We validate in build() because rules that involve several fields, such as the pool size and the retry count, can only be checked once all values are known. The cost is code: the builder repeats every field, which is about 60 extra lines for 15 fields.
4A Record With a Compact Constructor
A record is a class whose purpose is to carry data. Java generates the constructor, the accessors, equals(), hashCode() and toString(). A compact constructor is a constructor without a parameter list that runs before the fields are assigned, so it can validate and normalise the values.
Records are concise and need no extra tools. But there is a catch for our case: the canonical constructor takes all 15 values, so every caller must pass all of them, defaults included, and we are back to the readability problem of telescoping constructors. We can add shorter constructors that delegate to the canonical one, but that is telescoping again. So a record alone is a good fit when the caller naturally has all values, for example when the object is mapped from a configuration file, and a weak fit for hand-written calls.
5With-Style Copy Methods
With methods return a copy of an immutable object with one field changed, for example config.withRetries(5). They make variations easy: we can keep one base configuration and derive a test variant or a per-tenant variant from it. On a record we write them by hand, each one calling the canonical constructor, so the compact constructor still validates every copy.
Java has a proposal for derived record creation, a with expression on records (JEP 468), but it is not a final language feature as of JDK 25, so today we write the methods ourselves or generate them. With methods also do not solve the first construction: something still has to create the base object with all 15 values. They complement a builder; they do not replace it.
6Step Builders and the Final Choice
A step builder goes one step further than putting required fields in the builder's constructor. Each required field gets its own interface, so the compiler forces the caller to call host(), then port(), then user(), then timeout(), and only then offers the optional setters and build(). It is the strongest guarantee, at the cost of four extra interfaces.
For this question I would choose a builder over an immutable record, with the four required fields enforced either in the builder's factory method or with a step builder if the object is part of a public library API. The record's compact constructor still validates, so even code that bypasses the builder, such as a configuration binder, cannot create an invalid object. I would add with methods only for the two or three fields that callers actually vary.
| Option | Readable calls | Defaults | Required fields enforced |
|---|---|---|---|
| Telescoping constructors | No, positional values of the same type | Yes, but only in fixed combinations | Yes, by position |
| Builder | Yes, named methods | Yes, in one place | In the builder constructor or build() |
| Record with compact constructor | No for 15 values | No, caller passes all | Yes, every field is passed |
| With methods | Yes for variations | Need a base object first | Validated on every copy |
| Step builder | Yes | Yes | By the compiler, in order |
7Follow-up questions
Why not just use Lombok @Builder?
It removes the hand-written builder code, and for an internal class that may be fine. For a public configuration class in a library, though, it does not by itself enforce required fields, it hides the defaults behind annotations such as @Builder.Default, and it ties the build to a compiler plugin. A hand-written builder lets us document each method and decide exactly what build() checks.
Should the builder validate in each setter or only in build()?
Simple checks on one value, such as a negative pool size, can fail fast in the setter, which gives the clearest error. Rules that involve several fields can only be checked in build(). Most teams do both, and the record's compact constructor repeats the essential checks so that no path creates an invalid object.
8Conclusion
In this answer, we compared four ways to construct a configuration with 15 fields. Telescoping constructors become unreadable and let same-typed values be swapped. A record with a compact constructor validates well but forces every caller to pass all values. With methods make variations easy but need a base object, and derived record creation is still not final in JDK 25. A builder gives named calls, defaults in one place and validation in build(), and with required fields in its constructor or a step builder it is the choice for this case.
Open with
With 15 fields and only four required, I would not use telescoping constructors, because many parameters of the same type become positional guesses and swapping two ints compiles fine.
Then, if they want more
A record on its own is concise and its compact constructor is a good place to validate and normalise, but every caller would have to pass all 15 values, including the defaults.
With methods are nice for deriving variations from a base config, but they need the base object first, and the with expression for records is still only a proposal, not final in JDK 25. So my choice is a builder that produces an immutable record.
The four required values go into the builder's factory method, so nobody can forget them, the eleven defaults live in the builder fields, and build() checks rules that span several fields. The record's compact constructor repeats the essential checks, so a configuration binder that bypasses the builder still cannot create an invalid object.
If this is a public library type and the required fields are easy to confuse, I would consider a step builder, accepting the extra interfaces for the compile-time guarantee.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A configuration object has 15 fields, four required and the rest optional with defaults....": https://scrivio-interview-library.netlify.app/java/creational-patterns/a18f35e1c8/