Immutability 00:00:00 Make a prediction You will build a value that stays unchanged after publication, including the collections and keys it references. A finished invoice keeps the caller's list of two line items. The caller adds a third. Is the invoice immutable just because its list field is final? No. A final reference still points at a mutable list. Prevent changes through that alias and consider whether each element can also change. 00:00:27 Lee and the glass flowers A glass flower cast in one piece does not grow new petals. But setting a glass flower in a vase with a replaceable paper label means the label can still change. Fixed outer structure does not freeze every referenced object. Decide which nested state forms the published value and protect it all. An immutable object has no visible change after its constructor has finished. Every field keeps its value, and everything reachable through the fields keeps its value too. A method that sounds like a change, such as plus, returns a new object. Most surprising bugs need two things: sharing and change. Two parts of a program hold the same object, one of them changes it, and the other is wrong without having done anything. Remove either ingredient and the bug cannot happen. Immutability removes change, so sharing becomes free. 00:01:22 Keep yesterday's invoice unchanged Create an invoice from two draft line items. If it retains the caller's mutable list, appending a third item changes a supposedly finished invoice. Copy or freeze the needed data, including nested mutable values. Publish the invoice only after validation. To revise it, create a new invoice value and retain the original. Tests should confirm that the old total remains available after the new version exists. Create the revised invoice, add its third line and then print the original two-line version again. It must still have the old total. If both invoices point to the same mutable list, a correct-looking copy of the outer object cannot protect yesterday's result. 00:02:08 Use values as stable keys A map indexes a price by amount and currency. If those fields change while the value is a key, lookup may fail because its hash changes. An immutable money value keeps equality and hashing stable for its whole use as a key. Use a money value as a hash key and find its entry. Now imagine changing the amount in place. If its hash changes, the existing entry may be in a different bucket from the one used for lookup. An immutable key preserves that lookup invariant. Two threads can still overwrite the reference to different immutable versions and lose an update. Immutability protects values, not arbitrary updates to shared references. Use a lock, an atomic replacement or explicit version check when multiple writers coordinate on the latest version. Java String. The documentation says that strings are constant and that, because they are immutable, they can be shared. Literals are interned for that reason. java.time. LocalDate, Instant and Duration are documented as immutable and thread safe. They replaced Date, which has setters. plusDays returns a new date. 00:03:24 Try an interview follow-up Your team wants all classes immutable by rule. Your view? I would favour immutable values for messages, events and configuration because many readers can share them safely. For an entity that changes over time, I would insist on one owner and validated operations. I would use profiles and incident evidence to decide exceptions on a hot path instead of imposing a single rule everywhere. A booking record marks its guest list field as final. Later the original list gains a guest and the supposedly fixed booking changes. Capture an immutable collection, and account for whether its elements can change. Test an edit to both the source list and one shared element. 00:04:12 What to take with you A change builds a new object, the old one stays. This removes the pair that causes most surprises, sharing together with change. It gives free sharing between threads, safe keys, simple undo and checks that run once. The freeze must be deep: no setters, fields assigned once, copies of changeable inputs and outputs, and no subclass that breaks the promise.