Data Models

Learn about the standard functionality of data classes and their purpose.

Data classes

Data classes are used to hold data. They’re marked with the data keyword to tell the compiler that we’re creating this class to hold data so that it can automatically create the necessary functions to handle and process data.

Press + to interact
data class User(val name:String, val age:Int)

A data class contains state and fields. For a class to be considered a data class, the following must be true:

  • The primary constructor should have at least one parameter.
  • Each parameter must indicate if it’s a val or var.
  • Data classes cannot be abstract, sealed, open, or inner. They can extend other classes.

The data class methods

The data class has methods that are automatically generated when the ...