Data classes

Each time we create a model class, we end up writing the same boilerplate code for:

  • Constructor
  • Getter-Setter (s)
  • hashCode()
  • equals()
  • toString()

Kotlin introduced the  data keyword where the compiler automatically derives the following stuff based upon the parameters of the primary constructor:

  • Getter-Setter (s), which are not technically due to data keywords
  • equals()/hashCode() pair
  • toString()
  • componentN() functions corresponding to the properties in their order of declaration
  • copy() function

It saves a lot of boilerplate code and makes the model classes look clean and concise.

The Message class is the data structure representing the messages left by a user on the map. It holds the actual message left, the author of the ...

Get Kotlin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.