Quick revision
The short answer
A canonical constructor with eight arguments is error-prone because arguments of the same type can be swapped without any compiler error. A hand-written builder gives full control but is boilerplate to maintain, Lombok @Builder works on records but ties the build to Lombok, an annotation-processor builder such as RecordBuilder generates standard source code, and wither methods such as withTimeout(...) suit small changes to an existing value. For a shared API I would choose a hand-written or annotation-processor builder plus a few withers, because their code is stable and visible, rather than tooling that every consumer and contributor must also adopt.
What to remember
In this answer, we saw that an eight-component canonical constructor invites silent argument swaps when several components share a type. A hand-written builder gives full control at the cost of maintenance, Lombok @Builder removes the boilerplate but ties every build to Lombok and its JDK support, an annotation-processor builder such as RecordBuilder generates visible, standard source, and withers handle small changes to existing values. For a shared API, we prefer stable code we can see, hand-written or generated by a standard processor, with validation in the compact constructor.
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.
A canonical constructor with eight arguments is error-prone because arguments of the same type can be swapped without any compiler error. A hand-written builder gives full control but is boilerplate to maintain, Lombok @Builder works on records but ties the build to Lombok, an annotation-processor builder such as RecordBuilder generates standard source code, and wither methods such as withTimeout(...) suit small changes to an existing value. For a shared API I would choose a hand-written or annotation-processor builder plus a few withers, because their code is stable and visible, rather than tooling that every consumer and contributor must also adopt.
Why this question is asked
Records made immutable data classes short, but they arrived without builders or named arguments, so a record with many components forces callers into long positional constructor calls. In a shared API that many teams call, the choice of how to construct and modify such a record becomes part of the API's stability. Interviewers ask this design question to see whether we can compare the options on real costs, not just preference. In this answer, we will see why eight positional arguments are risky, then look at each option in turn with code, compare them, and finish with the choice for a shared API.
1Why Eight Positional Arguments Are Risky
A record's canonical constructor takes its components in declaration order. When several components share a type, the compiler cannot tell them apart, so swapping two of them compiles and runs, and the mistake shows up only in behaviour.
Three pairs are swapped in the second call, and nothing complains. At a call site with eight bare values, reviewers cannot see the mistake either. Every option below aims to attach a name to each value.
2Option 1: A Hand-Written Builder
We can nest a builder class inside the record. It holds a mutable field per component, with defaults where sensible, fluent methods that return this, and a build() method that calls the canonical constructor, where the compact constructor validates.
The control is complete: required values in the factory, defaults where we choose, documentation on each method, and nothing beyond the JDK. The cost is about 40 lines that must change every time a component is added, and the single line in build() is itself a positional call, so it must be reviewed carefully once. A unit test that builds with every method and checks every accessor catches mistakes there.
3Option 2: Lombok @Builder
Lombok is a compiler plugin that generates code from annotations during compilation. Recent versions support @Builder on records, and @Builder(toBuilder = true) also adds a toBuilder() method for copies with changes.
It removes all the boilerplate. But there are costs. Lombok hooks into javac internals, so each new JDK needs a matching Lombok release, which can delay upgrades. Everyone who builds the library, and their IDE, needs Lombok set up. The generated API is decided by the Lombok version and its configuration, so upgrading Lombok can change our public methods. And defaults and required values need extra annotations, such as @Builder.Default, which behave differently on records; check your version. Consumers of the compiled JAR do not need Lombok at run time, but they read a Javadoc and source that do not show the methods they call unless the build runs delombok.
4Option 3: An Annotation-Processor Builder
Annotation processors are a standard part of javac: they read annotations at compile time and generate new source files, without modifying existing classes. RecordBuilder is a popular open-source processor for records: annotating the record with @RecordBuilder generates a PoolSettingsBuilder class with fluent setters, a static builder() method, and an optional With interface that adds wither methods to the record.
The generated code is ordinary Java source in the build output, visible to IDEs and Javadoc, and it uses only the standard processor API, so it does not break on new JDKs the way javac-internal plugins can. The annotation itself is only needed at compile time. The remaining cost is one more build dependency for the library's contributors.
5Option 4: Wither Methods
A wither is a method that returns a copy of the record with one component changed, like withReadTimeout(Duration). It does not replace a builder for creating an object from scratch, but it is the most readable way to make a small change to an existing value, which is common for settings: start from defaults and adjust one or two.
| Option | Boilerplate | Dependency | Best for |
|---|---|---|---|
| Canonical constructor only | none | none | small records, internal use |
| Hand-written builder | high, maintained by us | none | public APIs needing full control |
Lombok @Builder | none | Lombok in every build | internal code already using Lombok |
Annotation processor (RecordBuilder) | none, generated source | processor at compile time | public APIs with many records |
| Withers | one method per component | none or generated | small changes to existing values |
Hand-written withers have the same positional-call risk inside them as build(), and there is one per component. The Java team has explored derived record creation, a with expression for records, as a future language feature (JEP 468); it is not part of a released JDK at the time of writing, so check your version.
6The Choice for a Shared API
For a shared API that many teams depend on for years, I value stability and visibility over saving lines. The builder's methods are part of the public contract, so they should be code we can see, review and keep stable across upgrades. That points to a hand-written builder when there are only a few such records, or an annotation-processor builder when there are many, plus withers for the components callers commonly adjust.
I would avoid Lombok specifically in the public API of a shared library, not because it is bad, but because it makes the public surface depend on a tool's version and ties the library's JDK upgrades to the tool's release cycle. Inside a single service that already uses Lombok, @Builder on a record is a perfectly reasonable choice.
7Follow-up questions
Could we split the record instead of adding a builder?
Often, yes, and it is worth asking first. Eight components may really be two concepts, such as an endpoint (host, port, database, user) and a policy (timeouts, pool size, retries). Two smaller records with three or four components each are readable with plain constructors and can be reused separately.
Where does validation go with a builder for a record?
In the record's compact constructor. Every path, the builder, the withers and direct constructor calls, ends there, so a single set of checks covers all of them. The builder should not duplicate the checks, or the two copies will drift apart.
Do withers allocate too much?
Each wither creates one new record, which is a small, short-lived object and cheap for the JVM. Changing four values with four chained withers creates three throw-away objects; if that pattern is common, a toBuilder() method is cleaner than chaining.
8Conclusion
In this answer, we saw that an eight-component canonical constructor invites silent argument swaps when several components share a type. A hand-written builder gives full control at the cost of maintenance, Lombok @Builder removes the boilerplate but ties every build to Lombok and its JDK support, an annotation-processor builder such as RecordBuilder generates visible, standard source, and withers handle small changes to existing values. For a shared API, we prefer stable code we can see, hand-written or generated by a standard processor, with validation in the compact constructor.
Open with
The problem with eight positional components is that several of them share types, two Strings, two Durations, two ints, so a caller can swap them and the compiler will not notice, and reviewers cannot see it at the call site either.
Then, if they want more
A hand-written builder nested in the record solves that with named methods, required values in the factory and defaults, and it has no dependencies, but it is around forty lines to keep in sync with the components.
Lombok's @Builder works on records and removes the boilerplate, including toBuilder for copies, but every build of the library needs Lombok, Lombok has to catch up with each new JDK because it uses javac internals, and our public methods then depend on Lombok's version.
An annotation processor like RecordBuilder generates a normal builder source file and wither methods through the standard processor API, so the code is visible and stable. Withers such as withReadTimeout are ideal for adjusting one value of an existing settings object, but they do not replace a builder for creating one.
For a shared API I would choose either a hand-written builder, if there are only a few such records, or a standard annotation processor if there are many, plus withers for commonly changed fields, with all validation in the compact constructor. I would also check whether eight components is really two smaller records.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Records have no built-in builder. For a record with eight components, compare a...": https://scrivio-interview-library.netlify.app/java/immutability/a3998c4e7d/