Data Class

Reading Time: 4 minutes

While building any application, we often need to create classes whose primary purpose is to hold data/state. These classes generally contain the same old boilerplate code in the form of getters, setters, equals(), hashcode() and toString() methods.

Kotlin has a better solution for classes that are used to hold data/state. It’s called a Data Class. A Data Class is like a regular class but with some additional functionalities.

With Kotlin’s data classes, you don’t need to write/generate all the lengthy boilerplate code yourself. The compiler automatically generates a default getter and setter for all the mutable properties, and a getter (only) for all the read-only properties of the data class. Moreover, It also derives the implementation of standard methods like equals(), hashCode() and toString() from the properties declared in the data class’s primary constructor.

Example Customer data class

Syntax of data class

Kotlin Data Class Requirements

      • The primary constructor must have at least one parameter.
      • The parameters of the primary constructor must be marked as either val (read-only) or var (read-write).
      • The class cannot be open, abstract, inner or sealed.
      • You need to append the class with the keyword data

Kotlin Data Class Features

      • To create a parameterless constructor, specify default values to each of the parameters present in the primary constructor.
      • A Data Class allows subclassing (No need to mention
        the keyword open).
      • You can provide explicit implementations for the functions equals() hashCode() and toString()
      • Explicit implementations for copy() and componentN() functions are not allowed.
      • We can control the visibility of the getters and setters by specifying the visibility modifiers in the constructor as shown below.

Default And Named Arguments in Data Class

Default And Named Arguments in Data Class
None of the parameters have a default value set. So we need to set an argument for each of them in the instantiation as shown below.

Instead of setting each argument, we can set only the non-default ones and the ones which we wish too using the named argument.

Using Named Arguments, we can set the 5th argument as the second one by explicitly specifying the parameter name followed by ‘=’.

Set non-default arguments

Named Arguments

Kotlin Data Class toString() Method

Book(name=Kotlin tutorials, authorName=Peter, lastModified=1234567, rating=4.5, downloads=1000)
Book(name=Kotlin, authorName=Peter, lastModified=1234567, rating=5.0, downloads=1000)
Book(name=Swift, authorName=Peter, lastModified=1234567, rating=5.0, downloads=500)
Book(name=Java, authorName=Lili, lastModified=1234567, rating=5.0, downloads=1000)
Book(name=Python, authorName=Viktória, lastModified=1234567, rating=5.0, downloads=1000)
Note that the print function implicitly adds a toString().

Kotlin Data Class copy() Method

Copy function is used to create a copy of an instance of the data class with few of the properties modified.

It’s recommended to use val parameters in a data classes constructor in order to use immutable properties of an instances.

Hence to create a copy of a immutable object by changing only few of the properties, copy() function is handy.

Book(name=Kotlin tutorials, authorName=Peter, lastModified=1234567, rating=4.5, downloads=1000)
Book(name=Kotlin, authorName=Peter, lastModified=1234567, rating=4.5, downloads=1000)

Kotlin Data Class equals() and hashCode()

The hashCode() method returns hash code for the object. If two objects are equal, hashCode() produces the same integer result. Hence, equals() returns true if the hashCode() is equal, else it returns a false.

Hashcode is 649213087
Hashcode is 1237165820
Hashcode is 649213087
copyBook and book are equal
newBook and book are NOT equal

The first and third object hashcodes are equal hence they are equal.

Note that the equals() method is equivalent to == in Kotlin.


I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂



Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

3 thoughts on “Data Class”

  1. I have observed that service fees for on-line degree specialists tend to be an excellent value. For example a full 4-year college Degree in Communication from The University of Phoenix Online consists of 60 credits from $515/credit or $30,900. Also American Intercontinental University Online provides a Bachelors of Business Administration with a entire study course feature of 180 units and a price of $30,560. Online studying has made obtaining your certification been so detailed more than before because you could earn your current degree through the comfort in your home and when you finish from work. Thanks for all your other tips I have really learned from your web-site.

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications