Campbell Ritchie wrote:Can you really not hand‑code immutable classes to provide all the safeguards you require?
You can properly hand-code a class to be immutable. What you can't protect against is change over time: someone adding a new set..() method or adding a field and forgetting that all important final. The platform doesn't know that this class was meant to be immutable, so it can't check that your correct code remains correct.
Campbell Ritchie wrote:Can't a record be mis‑written to have a mutable reference type as a field without taking defensive copies wheneve it is used?
Actually you can't if we're talking about the mutability of the field itself. If you try to add instance fields on a record type it will give you a compilation error.
If you're meaning that the object referred to by a field can be mutable still, that is sadly a hole in enforcing immutability that Java still has and records don't help you unless you use them consistently everywhere.