简体   繁体   中英

Writing to a Record field in Java 14

From a brief search on intro's to Records in Java 14, I see that Record fields are compiled as final instance variables. This seems to imply I can't write to Record fields.. none of the tutorials mention anything about writing to Record fields either.

Why is that? Why can't I use a Record in Java 14 like I would use a struct in C?

Thanks for any insight

Records provide a compact syntax for declaring classes which are transparent holders for shallowly immutable data

...

The components of a record are implicitly final. This restriction embodies an immutable by default policy that is widely applicable for data aggregates .

Source

Being immutable, at least shallowly, is precisely the point. Records are designed as value types, so no, you cannot change them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM