Data classes in Kotlin are primarily used to hold data. For each data class, the compiler automatically generates additional member functions that allow you to print an instance to readable output, compare instances, copy instances, and more. Data classes are marked with
The compiler automatically derives the following members from all properties declared in the primary constructor:
.toString()of the form
.componentN()functions corresponding to the properties in their order of declaration.
.copy()function (see below).
To ensure consistency and meaningful behavior of the generated code, data classes have to fulfill the following requirements:
The primary constructor must have at least one parameter.
All primary constructor parameters must be marked as
Data classes can't be abstract, open, sealed, or inner.
Additionally, the generation of data class members follows these rules with regard to the members' inheritance:
If there are explicit implementations of
.toString()in the data class body or
finalimplementations in a superclass, then these functions are not generated, and the existing implementations are used.
If a supertype has
.componentN()functions that are
openand return compatible types, the corresponding functions are generated for the data class and override those of the supertype. If the functions of the supertype cannot be overridden due to incompatible signatures or due to their being final, an error is reported.
Providing explicit implementations for the
.copy()functions is not allowed.
Data classes may extend other classes (see Sealed classes for examples).
Properties declared in the class body
The compiler only uses the properties defined inside the primary constructor for the automatically generated functions. To exclude a property from the generated implementations, declare it inside the class body:
In the example below, only the
name property is used by default inside the
.copy() implementations, and there is only one component function,
age property is declared inside the class body and is excluded. Therefore, two
Person objects with the same
name but different
age values are considered equal since
.equals() only evaluates properties from the primary constructor:
.copy() function to copy an object, allowing you to alter some of its properties while keeping the rest unchanged. The implementation of this function for the
User class above would be as follows:
You can then write the following:
Data classes and destructuring declarations
Component functions generated for data classes make it possible to use them in destructuring declarations:
Standard data classes
The standard library provides the
Triple classes. In most cases, though, named data classes are a better design choice because they make the code easier to read by providing meaningful names for the properties.