Skip to content

Instantly share code, notes, and snippets.

@pavelfomin
Last active March 27, 2024 21:16
Show Gist options
  • Save pavelfomin/6c9f8a6595e1d2580dafa74e225ade0f to your computer and use it in GitHub Desktop.
Save pavelfomin/6c9f8a6595e1d2580dafa74e225ade0f to your computer and use it in GitHub Desktop.
Java Record

Things I don't like about java records

  • record is final so it
    • can't be used in inheritance
    • can't be mocked in unit tests
  • doesn't support builder pattern and as the number of fields grows Lombok's @Builder becomes preferable
    • although it seems to be possible to use Lombok's @Builder with java record

I don't see any major advantages with using record other than its immutable nature.

Java record

import lombok.Builder;
@Builder
record FooRecord(
    String name,
    String city,
    String zip) {
}

Java class

import lombok.Builder;
import lombok.Data;
@Data
@Builder
class FooData {
    String name;
    String city;
    String zip;
}

See also https://www.baeldung.com/java-record-vs-lombok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment