How to use data classes in Kotlin
Tags: kotlin, android
Published: 2020-07-07 23:17:13

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:

```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;
    }
}
```

This is what Kotlin is offering to us:

```kotlin
data class Song (var title: String, var minutes: Int)
```
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**: 

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

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.

```kotlin
data class Dog (var name: String, var age: Int, var isFriendly: Boolean = true)
```

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

```kotlin
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) 
```

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.

```kotlin
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
```

###ToString()

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

```kotlin
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)
```

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

```kotlin
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
```


###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:

```kotlin
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)
```

###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

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

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

 println(name + " " + age)
// output -> spoon 1
```



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.