Constructors 00:00:00 Make a prediction You will make a constructor publish only a valid object, and place fallible external setup where the caller can handle the result. A booking begins at eleven and ends at ten on the same day. If construction succeeds and a later validation method rejects it, what can other callers observe in between? They can see an impossible booking. Validate the start and end together before publishing the object. When construction succeeds, every operation can rely on the agreed range rule. 00:00:33 The scarecrow on order A scarecrow is assembled on the bench. Missing a support pole, it falls as soon as it reaches the field. Finish and inspect the load-bearing pieces before handing it to the farmer. During construction, a partially filled object can be workable internally. Once it is handed to callers, its required fields and cross-field invariants should already hold. A constructor is the code that runs when an object is created. It takes the arguments, checks them and sets every field. After it has finished, the object is valid and may be used. Without a constructor, each caller must remember to set every field in the right order. One forgotten field is a bug that shows up far away from where it was made. 00:01:17 Build a valid date range A caller supplies a booking with a start after its end. If a half-initialized object escapes, every later method must defend itself. Validate both values together during creation and reject an invalid interval before publishing the object. A successful caller receives a booking whose start precedes its end. Test equal bounds separately because the business rule must decide whether a zero-length booking is allowed. The invalid range must fail before the booking enters a calendar. Reverse the inputs to ten and eleven, and now both endpoints can be observed as a valid pair. If a setter could update them one at a time without checking, the constructor's guarantee would disappear immediately. 00:02:01 Keep external work out of setup A report object needs a storage client. Passing an existing client into its constructor makes its dependencies visible. Opening a network session inside construction turns a simple unit test into an integration test. If opening can fail, use an explicit factory operation with a clear failure result. Disconnect storage while constructing the report. If creation secretly opens a network connection, even tests of formatting fail before they reach their assertion. Accepting a storage client lets a test construct the object offline and exercise failure explicitly when it asks to save. Starting a callback from the constructor can expose a partly initialized object to another thread. Validation at the end is too late if the object has already escaped. Use a named factory or explicit open operation if creating the resource can fail and requires cleanup or a richer result. Java Integer.valueOf. The documentation calls the static factory generally a better choice than the constructor, because it caches: values from -128 to 127 always come from the cache. The constructor is deprecated for removal. Java List.of and List.copyOf. Static factories that return unmodifiable lists. The caller names what they want and never sees which class it is. 00:03:28 Try an interview follow-up How do you add a parameter to a public constructor that thousands call? I do not change it. If there is a sensible default, an overload or builder option is added. If not, a new factory, and the old one is deprecated with a date. In Java even a source-compatible change can break compiled callers, so I check with a compatibility tool and release a major version for the removal. Yesterday's saved booking lacks an end time after a schema change. Should deserialization bypass the constructor to make it load? Treat loading as a construction path. Migrate the record using an agreed rule when enough information exists, or reject and flag it. Bypassing validation produces exactly the invalid object that the public constructor was built to prevent. 00:04:21 What to take with you new reserves memory, the parent part is built, then this class's fields. The reference goes to the caller only at the end. Until then nobody else should see the object. Check arguments, set every field, copy what can change, and stop. No network, no calls to methods that a subclass may replace, no handing out this.