Using Java records in Kotlin

Edit pageLast modified: 25 September 2024

Records are classes in Java for storing immutable data. Records carry a fixed set of values – the records components. They have a concise syntax in Java and save you from having to write boilerplate code:

The compiler automatically generates a final class inherited from java.lang.Record with the following members:

  • a private final field for each record component

  • a public constructor with parameters for all fields

  • a set of methods to implement structural equality: equals(), hashCode(), toString()

  • a public method for reading each record component

Records are very similar to Kotlin data classes.