A Practical Guide to Kotlin Data Class — With Examples

TL; DR: Data class reduces boilerplate code by automatically providing getter/setter, hashcode, equals, toString, and componentN methods.

Kotlin provides a special type of class known as the data class, which has a prefix with the data keyword. An example of a data class is as below:

data class Song(val genre: String, val artist: String)

When to use Kotlin data class?

When we want to include the object properties in getter/setter, hashcode()/equals(), toString() and copy() functions, we can use a data class.

Only object properties declared in the primary constructor will be used.

With Song as our data class example, all of its properties in the primary constructor ( i.e.genre, artist) are automatically used for :

  1. hashcode()/equals()
val songA: Song = Song("Country", "Jim")
val songB: Song = Song("Country", "Jim")
val isEqual = songA.equals(songB) // true

2. toString()

val song: Song = Song("EDM", "Jessie")
print(song.toString()) // "Song(genre=EDM, artist=Jessie)"

3. copy()

The copy()function creates an equal copy of the object. But we can see that they are different instances. It’s also useful when we want to have a copy with certain different properties

val song: Song = Song("Jazz", "John")
val anotherSong: Song = song.copy()
print(song == anotherSong) // true
print(song === anotherSong) // false
val nextSong: Song = song.copy(artist = "Judy")

4. componentN() which allows deconstruction

Kotlin will generate componentN functions based on the properties in primary constructors. What this means is, we can deconstruct object like so:

val song: Song = Song("Metal", "Jessica")
val (theGenre, theArtist) = song;
print(theGenre) // "Metal"
print(theArtist) // "Jessica"

Data Class Equivalent in Java

With just a single line of code, data class provides us all the functionalities and behaviours above. In Java, a Song class with similar behaviours would look like this:

Useful tricks and tips

If we want to exclude any property from being included in the automatically generated functions, we can put the property in the class body :

Only genre and artist properties will be used in the implementation of hashcode(), equal(), copy(), toString() method.

It is recommended to use val (similar to final in Java) for the properties declared in the primary constructor for the data class to be immutable

We can of course have var in the primary constructor properties as well but this would make data class mutable.

data class Song(val genre: String, val artist: String, var title: String)val songA: Song = Song("Pop", "Jamie", "To the moon")
songA.title = "To the sun"

Gotchas and things to take note

  1. data class cannot be abstract, inner, open or sealed.
  2. At least 1 parameter is required in the primary constructor.
  3. data class does not implement Serializable by default.
  4. data class can implement interfaces and extend other classes (since v1.1).

If you find this post helpful, help others to see it by giving some claps 👏

Full-stack Web Developer | Twitter @ https://twitter.com/FrontEndJunkie

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store