<html><head>
<style id="css_styles">
blockquote.cite { margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc }
blockquote.cite2 {margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc; margin-top: 3px; padding-top: 0px; }
a img { border: 0px; }
li[style='text-align: center;'], li[style='text-align: center; '], li[style='text-align: right;'], li[style='text-align: right; '] { list-style-position: inside;}
body { font-family: 'Segoe UI'; font-size: 12pt; }
.quote { margin-left: 1em; margin-right: 1em; border-left: 5px #ebebeb solid; padding-left: 0.3em; }
</style>
</head>
<body>
<div>Hi,</div><div><br /></div><div>I have tried googling this topic and looking through the amber-dev and amber-spec-experts archives, but was unable to locate anything that mentioned transient fields in combination with records. The JEP itself also makes no mention of transient fields. The word transient seems to never be mentioned in combination with records anywhere...</div><div><br /></div><div>I often enough design classes for serialization in some fashion or other. One of the tools that one can use here is to use transient fields to store information that can be completely derived from the other fields. These are used to store information that may be too large to store or too expensive to (re)calculate on every access. All major frameworks that do serialization recognize such fields and act accordingly (Java serialization, Jackson, Hibernate).</div><div><br /></div><div>However, records do not allow such fields, even though I think one of their primary use cases is data storage, which often will sooner or later involve serialization. Now I can't imagine this was an oversight, so I'm curious to know if transient fields were simply left out for now, or that they are not allowed for other reasons.</div><div><br /></div><div>Javac simply complains: TransientRecord.java:4: error: field declaration must be static
</div><div><br /></div><div>Here is the use case I had when I encountered this problem:</div><div><br /></div><div> public record Message(
A a, <span>B b, </span><span>C c, </span><span>D d) {</span></div><div><span> public enum Type { </span></div><div> A, B, C, D;
</div><div> }
</div><div>
</div><div> private final transient List<Type> types;
// <<< not allowed</div><div>
</div><div> public Message {
</div><div> List<Type> types = new ArrayList<>();
</div><div>
</div><div> if(a != null) {
</div><div> types.add(Type.A);
</div><div> }
</div><div> if(b != null) {
</div><div> types.add(Type.B);
</div><div> }
</div><div> if(c != null) {
</div><div> types.add(Type.C);
</div><div> }
</div><div> if(d != null) {
</div><div> types.add(Type.D);
</div><div> }
</div><div>
</div><div> this.types = Collections.unmodifiableList(types);
</div><div> }
</div><div>
</div><div> public List<Type> getTypes() {
</div><div> return types;
// use transient field to avoid recalculation</div><div> }
</div><div> }</div><div><br /></div><div>--John</div></body></html>