From the course: Kotlin Essential Training: Object-Oriented and Async Code

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Data classes

Data classes

- [Instructor] When writing object oriented code, it's common practice to have types that are simple value holders, that have no real business, logic, or purpose other than to hold some set of data. Data classes in Kotlin are specifically designed for this purpose and have several useful traits that help reduce boilerplate code. We can define a data class, much like a regular class, but using the data modifier and with a few restrictions. So let's open up our Project pane here, and we're going to create a new Kotlin class or file, and we are going to select the Data class option, and we will name this class Task. So this is now created a new Task.kt file. And with it, a data class named Task. Notice the data modifier in front of the class name. This is what differentiates a class from a data class. Now, by adding that data modifier, we bring with it some restrictions that data classes have. And one of those special…

Contents