Introduction and Comparison to JAVA

In JAVA when you want to create a simple POJO class , you have to do a lot of ceremony, write getter/setter for all properties, write equal, toString and hashCode function. While the verbosity of it is good in some cases, writing and maintaining such unnecessarily huge classes is tough. Kotlin introduces Data class to tackle this problem and make the life of developer a little bit more easier.

Prerequisite of this tutorial is that you must have basic understanding of classes in Kotlin.

Below is a simple ‘MovieDetail’ class in JAVA with getters and setters for every field.

Data Class Magic!

Now below is the same class in Kotlin using Data class.

Unbelievable Right! To create a data class, you have to use the data keyboard and append in front of your class declaration. That is it!

Data class provides you with everything, getters, setters, hashCode, toString and equals functions. So all you have to do is create an instance and start using the functions.


Components is another way that Kotlin provides to access the class fields. Theses are set of functions with which you can access all the properties in the sequence they are defined. The syntax is very simple, the name “component” followed by the sequence number, componentX(), where X is the sequence number of property you want to access. So for this example:

  • component1() will return the value stored in variable name.
  • component2() will return the value stored in variable rating.
  • component3() will return the value stored in variable director.
  • component4() will return the value stored in variable releaseDate.

If you try to go above the sequence, e.g. component5() then the compiler will throw an error because there is no field with sequence number 5.

Destructuring in loops

In kotlin you can destructure individual objects when iterating over loops, so you wouldn’t need to access the fields by using the object each time (rather than writing you can directly use name).

Here you can see, inside the for loop, rather than writing a single name such as movie and then calling,, we directly destructured the fields by writing (name, rating, director, date). Remember, the fields can only be accessed in the order in which they were defined. So you cannot access rating before name, you would have to provide a variable for name and then will you be able to provide one for rating.

Read/Write Fields vs Read only Fields

This is not related to just Data classes, but also to normal class and variable definition, but I thought it might be good to cover it here.

When you define a field, you have two options while declaration, val or var. Here is a simple definition for both.

val (Read Only) – When you define a field using val, you essentially are saying that this field is read only and once its value is assigned it cannot be changed. In our MovieDetail example, we have defined the variable releaseDate using val, once the movie is released, there is no way its release date is going to change, so there is no point in giving the ability to change value once assigned.

var (Read/Write) – When you define a field using var, you essentially are saying that this field is readable as well as writeable, so a new value can be assigned to it at any point of time. In our MovieDetail example, we have defined the rating field using var, as more and more people watch the movie, the rating will change eventually, so we have provided the ability to change the value whenever required.

That is all there is to Data classes in Kotlin, hope you have enjoyed the article.

More about functions in Kotlin

Installation and Hello World in Kotlin

Basic Syntax, Variables and Data Types in Kotlin

Control Flow in Kotlin

Looping in Kotlin

3 Responses

  1. Roi

    great article.
    but the same class in Kotlin is:
    data class MovieDetail(var name: String,
    var rating: Int,
    var director: String,
    var releaseDate: Date)

    • Gurleen Sethi

      You can also use var for all you properties, I used val because it only makes sense to change rating after a movie object is created, though I know what you are trying to say, the Java class had getters as well as setters for all fields so do the same in Kotlin class by defining every field with var.


Leave a Reply

Your email address will not be published.

2 + seven =