Class RecordPreviewExamples

java.lang.Object
net.jrodolfo.java_evolution.java14.RecordPreviewExamples

public class RecordPreviewExamples extends Object
Demonstrates records as a Java 14 preview feature.

Before records, simple immutable data carriers required constructors, fields, accessors, equals, hashCode, and toString. Most of that code was mechanical and easy to generate incorrectly.

Records solve this by letting the developer declare the data components and letting the compiler generate the standard value-based behavior. Records became final in Java 16. This project compiles on JDK 25, so this class uses final record syntax while documenting that Java 14 was the first preview release.

  • Constructor Details

    • RecordPreviewExamples

      public RecordPreviewExamples()
  • Method Details

    • feature

      public RecordPreviewExamples.Feature feature(String name, boolean preview)
      Creates a record value.
      Parameters:
      name - the feature name
      preview - whether the feature was preview in Java 14
      Returns:
      a record containing both values
    • describe

      public String describe(RecordPreviewExamples.Feature feature)
      Formats a record using its generated accessor methods.
      Parameters:
      feature - the feature to describe
      Returns:
      a readable description