records
record Target(String host, int port) {
String endpoint() {
return host + ":" + port;
}
}
A record is a class with the boilerplate generated
Constructor, accessors, equals, hashCode, and toString are provided
You still write methods
When to use
Use records for data carriers Use classes for objects with behaviour and mutable state Do not force behaviour into a record just because it is shorter
Immutability
Record components are private and final The record itself is shallowly immutable A mutable field inside a record component is still mutable
Next
What changed in 11 current
Posted