In Kotlin you can create a class like this:
data class Dragon(private val nombre: String, val type: Int)
What is the main goal of the data class
in Kotlin? When should we use?
In Kotlin you can create a class like this:
data class Dragon(private val nombre: String, val type: Int)
What is the main goal of the data class
in Kotlin? When should we use?
It was created for DTOs: Data Transfer Objects.
Many times we create classes that serve only and purely to pass data such as the class
Usuario
and creating them is a headache for something so simple:Although IDEs like android studio help with the generation of getters/setters, it's too much code for something as simple as transporting data.
Kotlin boils all that down to this:
And it automatically generates the getters and setters without the need for so many rituals.
As mentioned at the beginning, data classes are only for classes that serve information and that's it, without any logic involved.