Demystifying Kotlin Data classes Part I

Shivam
3 min readJan 11, 2020

--

This is part I of the Demystifying Kotlin Data class series, over the series we will study the in and outs of the Kotlin Data classes. In this first part, we will look into what is the Data class and how it is better than the traditional java model/POJO class. Let’s start with the introduction.

Introduction:

While developing an application, we often need to create a class just to hold data. This data class generally contains only getters and setters, And some utility methods like equals(), hashCode() and toString(), etc.

The advantage of using data classes instead of regular classes is that Kotlin generates all these boilerplate codes for us. Which helps us to keep our code clean.

Let’s say we want to create a model class to store details related to our secret mission. Following is the Java model class for this:

Above Mission class contains only 2 fields and there is no utility method only getters and setters.

Now look at the Kotlin version of the same class:

Oh Yeah!, that’s all it requires and we would still be achieving the same/more amount of functionality. In the above java example, we haven’t added equals(), hashCode(), toString() and still our class contains code of more than 25 lines.

And in Kotlin we have achieved all of these in just one line of code. That is the power of the data class. Kotlin compiler automatically derives the following methods from all the properties declared in the primary constructor:

  1. equals()
  2. hashCode()
  3. toString()
  4. copy()
  5. componentN()

In the next part, we will dig deeper into what is the use of these methods and how they work.

How Can I Create The Data Class?

Let’s see how we can create a data class. There are some rules or restrictions that we should keep in mind about data class:

  1. The primary constructor can’t be empty. It makes sense — the purpose of the data class is to hold the data and if you don’t have any properties in it WTF¹ are you going to do with it?
  2. All the parameters of the primary constructor need to be marked either as the val or the var. It also makes sense — if we don’t use either val or var with the field in the primary constructor, then the scope of that field becomes limited to the init block only and It isn’t accessible in the other methods written in the same class forget about accessing it from outside of the class. We can have non-val or non-var parameters in the secondary constructor.
  3. Data classes can’t be open, sealed, inner, or abstract.
  4. Data class is the final class, it can’t be inherited.

Data class can extend open, sealed classes. And also can implement the interfaces.

# Val or Var

We can declare properties of a data class either as var(mutable) or val (immutable). The only difference between them is — setters will not be created for val properties. It is highly recommended that we should use val this will give us benefits while working with multi-threaded applications.

Conclusion

  • Data class can be used to avoid writing common boilerplate code.
  • Data class helps to keep our code clean and short.

Thanks. That’s all for today, hope this post helps you to learn more about what is Data class and how to use it. If you have any suggestions or questions, please add it in the comment below. Thanks again for reading, Happy Coding 👏.

Further Reading

I would recommend you to read Demystifying Kotlin Data classes Part II in that I provided a more detailed description of copy() and componentN() and data class generates the boilerplate code.

References

1 — WTF is What The Fun in my dictionary. 😺

--

--

Shivam

Product Engineer @ Gojek. Likes to write on Productivity, Android App Development, Kotlin, Software Engineering, etc.