DEV Community

Cover image for How to use data classes in Kotlin
Jess Barrientos
Jess Barrientos

Posted on

How to use data classes in Kotlin

Sometimes we need to create a class whose main purpose is only to hold data without any other functionality in it. Here a simple example of what we used to do in Java:

public class Song {

    private String title;
    private int minutes;

    public pojoExample(String title, int minutes){
        this.title = title;
        this.minutes = minutes;
    }

    public String getTitle(){
        return title;
    }

    public void setTitle(String title){
        this.title = title;
    }

    public int getMinutes(){
        return minutes;
    }

    public void setMinutes(int minutes){
        this.minutes = minutes;
    }
}
Enter fullscreen mode Exit fullscreen mode

This is what Kotlin is offering to us:

data class Song (var title: String, var minutes: Int)
Enter fullscreen mode Exit fullscreen mode

That’s it!.

So far, we can see that Kotlin helps us to not write useless code, and also looks cleaner.

Also Kotlin generate some functions automatically for us:

  • constructor
  • toString()
  • hashCode()
  • copy ()
  • componentN()

Nice!, Let’s take a closer look.

Imagine we have the next data class:

data class Dog (var name: String, var age: Int, var isFriendly: Boolean)
Enter fullscreen mode Exit fullscreen mode

So, first of all, we define the class Dog, which has 3 properties: name, age, and isFriendly. All of them start with “var” which means that the property can be reassigned. Then, we have the name of the property and the type. Also, we can add a default value to our properties.

data class Dog (var name: String, var age: Int, var isFriendly: Boolean = true)
Enter fullscreen mode Exit fullscreen mode

We set isFriendly as true by default, so is not necessary to add it in the definition of our instance:

var myDog0 = Dog("spoon", 1, false)  

var myDog1 = Dog(age = 10, isFriendly = true, name = "fork")

var myDog3 = Dog(age = 10, name = "cup")

var myDog4 = Dog("knife", 1) 
Enter fullscreen mode Exit fullscreen mode

Also, we can define our properties in the body of our class, so that means that we can't set the property value when we create an instance of a class, we can only set it directly in the object, once is created.

data class Cat(val name: String) {
     var age: Int = 0
 }

// how instance an object
var myCat = Cat("shampoo")
myCat.age = 10

println(myCat.age)
// output —> 10
Enter fullscreen mode Exit fullscreen mode

ToString()

This method converts the data class object to a string, listing all their properties:

var myCat = Cat("shampoo")
println(myCat.toString())
// output --> Cat(name=shampoo) 

var myDog0 = Dog("spoon", 1, true)  
println(myDog.toString())
// output --> Dog(name=spoon, age=1, isFriendly=true)
Enter fullscreen mode Exit fullscreen mode

We can also override this method in case we need something more customized:

data class Wine(val name: String, val age: Any, val brand: String){
     override fun toString(): String = name + " " + age + " " + brand
 }

var myWine = Wine(name = "Dicorno", age = 1, brand = "cheap")

println(myWine.toString())
// output—>  Dicorno 1 cheap
Enter fullscreen mode Exit fullscreen mode

Copy

Sometimes we need to copy a data class but changing one or more of their properties and keeping the rest intact. Copy function allows us to do that. Just we need to set the property that we want to change and Kotlin will do the rest.

Here an example:

data class Dog (var name: String, var age: Int, var isFriendly: Boolean)

// instance of our class Dog
var myDog = Dog(name = "spoon", age = 1, isFriendly =  true)  

// copy our class myDog, but changing only the name
val yourDog = myDog.copy(name = "Bull")

println(yourDog.toString())

//output —> Dog(name = Bull, age = 1, isFriendly = true)
Enter fullscreen mode Exit fullscreen mode

Destructuring Declarations

This is another helpful functionality that we can do with our data classes, we can get as variables the properties of one data class

var myDog = Dog(name = "spoon", age = 1)

// get the properties
 val (name, age) = myDog

 println(name + " " + age)
// output -> spoon 1
Enter fullscreen mode Exit fullscreen mode

So, As we can see data classes let us get cleaner code and (sometimes) forget about the boilerplate that we used to do in Java.

Top comments (1)

Collapse
 
fxmt2009 profile image
Ade

Excellent. Please find the time and write more.