Data classes in Kotlin

Data Classes in Kotlin: save a good bunch of lines of code (KAD 10)

We’ve already seen the classes in an earlier article, but data classes go a little further in helping us simplify our code.

What are data classes?

A data class is a class that only contains state and does not perform any operation.

The advantage of using data classes instead of regular classes is that Kotlin gives us an immense amount of self-generated code.

In particular, it gives us all this for free:

  • The properties declared in the constructor: this technically is not exclusive to a data class, but it avoids all the boilerplate of getters and setters, in addition to the constructor.
  • equals() / hashCode()
  • A set of functions called componentX(), which allow us to do a cool thing we’ll see later.
  • copy()method, very useful when we use immutable objects.

¿How is Java code compared to a data class?

Here comes the awesomeness. Although almost all this code is generated by the IDE, in Java we need this to implement a data class:

public class Person {

    private String name;
    private String surname;
    private String id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getSurname() {
        return surname;

    public void setSurname(String surname) {
        this.surname = surname;

    public String getId() {
        return id;

    public void setId(String id) { = id;

    @Override public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (name != null ? !name.equals( : != null) return false;
        if (surname != null ? !surname.equals(person.surname) : person.surname != null)
            return false;
        return id != null ? id.equals( : == null;


    @Override public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (surname != null ? surname.hashCode() : 0);
        result = 31 * result + (id != null ? id.hashCode() : 0);
        return result;

    @Override public String toString() {
        return "Person{" +
                "name='" + name + ''' +
                ", surname='" + surname + ''' +
                ", id='" + id + ''' +

And we would still be far from achieving the same amount of functionality that Kotlin provides with this line:

data class Person(var name: String, var surname: String, var id: String)

This is where we really see Kotlin’s potential, in the amount of useless code that saves us.

Classes destructuring

That’s the use of componentX functions. Thanks to them, you can decompose a data class in variables this way:

val person = Person("x", "y", "z")
val (n, s, i) = person

Thanks to this, you can do things like decomposing map pairs inside a loop:

val map = mapOf(1 to "a", 2 to "b")

for ((key, value) in map) {
    toast("key: $key, value: $value")

Objects copy

As we’ve talked before, it’s a good practice to us immutability in every possible situations. If we implement previous class as immutable:

data class Person(val name: String, val surname: String, val id: String)

If we now want to change the surname, we won’t be able.

When you work with immutability, to change the state of an object you need to copy it with the new value. And that’s the use of copy function:

val person = Person("John", "Smith", "123abc")
val person2 = person.copy(surname="Rogers")

The copy function can receive as many parameters as values you need to change. As you can see, function parameters can be named, so you can specify which ones you want to modify.


data classes saves a lot of boilerplate that Java forces us to generate, so you end up with a code that is easier to understand and to maintain.

If you like what you’ve seen, I encourage you to sign up for my free training, where I’ll tell you everything you need to learn about how to create your own Android Apps in Kotlin from scratch.