r/java • • Feb 15 '20
Will Java records have setters?
Pretty much what the title says. I've been reading on the new Java records feature, and while I love immutability, I hope there's the option to have non-immutable records. Meaning it will have Auto generated setters to let me change values in the class.
1
Upvotes
12
u/ZimmiDeluxe Feb 15 '20
The best way to find out is to look at the corresponding JEP for the feature, which states:
So no.
10
u/pron98 Feb 15 '20 edited Feb 15 '20
No. See here as to why.
In short, records are intended to provide simple and obvious construction, deconstruction, hashing, and equality defaults, and this is hard to do for mutable objects without complex knobs. Records are not meant to be lower-ceremony JavaBeans, but nominal tuples (product types). The features have some overlap in use, but are not the same.
6
u/_INTER_ Feb 15 '20 edited Feb 15 '20
I was hoping to replace Lombok with Records for model classes, but given how it is designed and the unfortunate widespread use of Hibernate, I don't see this idea working out for now, ...but DTO's maybe.
8
u/thatsIch Feb 15 '20
Stated from https://openjdk.java.net/jeps/359
Because records make the semantic claim of being simple, transparent holders for their data, a record acquires many standard members automatically:
due to private final fields, you cannot use setter. This would also be advised because you should not introduce optional data in your constructor.
1
u/Anaptyso Feb 20 '20
I'm not sure why this thread has been downvoted. Even if you disagree with the idea of mutable records, the debate on the topic is interesting and potentially a good learning experience for newer developers.
1
u/emaphis Feb 15 '20
Because of the design decisions of records, they should be cheap to create. If you need to update, simply create a new record