<div><div><br></div><div><div class="gmail_quote"></div></div></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)" dir="auto">First, I think you have the question backwards: it would be using public <br>
fields that would require a justification.</blockquote><div dir="auto"><br></div></div><div><div dir="auto">This is only true from the perspective of what is currently common practice in the Java community. But common practice in Java has changed a lot in the last few decades, often due to what becomes ergonomic with new project-amber-like features :)</div><div dir="auto"><br></div><div dir="auto">If we look at what record-like things look like in other languages and settings (C structs, ML records, Pascal records) accessors are the strange choice that needs justification.</div><div dir="auto"><br></div></div><div><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)" dir="auto">In any case, the answer is simple: not all objects are immutable. If a <br>
record component were an array, or an ArrayList, or any other object <br>
with mutable state, having public fields would make it impractical to <br>
use these types in records in many cases, because we'd be unable to <br>
expose their state without also exposing their mutability.</blockquote><div dir="auto"><br></div><div dir="auto">I think you are suggesting techniques like explicitly defining accessors for mutable record components that make defensive copies? Are we really comfortable calling records “a simple aggregation of values” if I have to read the documentation or implementation of an accessor just to understand what the following code does:</div><div dir="auto"><br></div><div dir="auto">someRecord.someArrayComponent()[0] = 42</div><div dir="auto"><br></div><div dir="auto">?</div></div></div>
</div>