Kotlin Idioms Creating DTOs (POJOs/POCOs)


Data classes in kotlin are classes created to do nothing but hold data. Such classes are marked as data:

data class User(var firstname: String, var lastname: String, var age: Int)

The code above creates a User class with the following automatically generated:

  • Getters and Setters for all properties (getters only for vals)
  • equals()
  • hashcode()
  • toString()
  • copy()
  • componentN() (where N is the corresponding property in order of declaration)

Just as with a function, default values can also be specified:

data class User(var firstname: String = "Joe", var lastname: String = "Bloggs", var age: Int = 20)

More details can be found here Data Classes.