Kotlin Data Class

Aug 12, 2020


Boilerplate. One of our biggest headaches. It’s up there with Hungarian notation. Let me show you what we’re talking about and create a class Client (Of course pun intended).

Let’s try and make this class safe to use, side-effect free and make it immutable. As stated in Item 15 Effective Java: “Classes should be immutable unless there’s a very good reason to make them mutable.”

final class Client {
    private final String name;
    private final String address;
    private final int age;


    public Client(String name, String address, int age) {  
        this.name = name;  
        this.address = address;  
        this.age = age;  
    }

    public String getName() {  
        return name;  
    }

    public String getAddress() {  
        return address;  
    }

    public int getAge() {  
        return age;  
    }  
}

Often with classes, we need to store them in Collections, and to do that correctly we’ll need to provide a correct implementation of the equals andlowin folg Item 9 of Effective Java, the hashCode function. Immutable value objects are very tedious to create in Java, because of this very reason.

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Client client = (Client) o;
    return age == client.age &&
    Objects.equals(name, client.name) && Objects.equals(address, client.address);
}


@Override  
public int hashCode() {  
    return Objects.hash(name, address, age);  
}

We also sometimes want to call toString on classes and get some sensible return from that method rather than just the address of the class. According to Item 12, Joshua says “when practical, the toString method should return all of the interesting information contained in the object.” Let’s add that as well:

@Override
public String toString() {
    return "Client [name=" + name + ", address=" + address + ", age=" + age + "]";
}

If anyone is counting, that simple class took 45376 lines of code. Oh, we forgot to add a phone number… And now we have to modify the functions as well.

Can Kotlin do better than this… hmm let’s see.

data class Client(val name: String, val address: String, val age : Int)

As a matter of fact, it can! And in just one line!

Under the hood, the compiler provides us with the hashCode(), equals(), toString() methods.

Enter the world where a programming language takes the responsibility of not just cutting down on boilerplate, but while doing it, it’s following some main principles taken from the Java “bible”.

Tanja Zlatanovska

Book a Free Consultation

Select your industry*

Please select your industry*

Select your service type

Please select your service type

When is the best time to get in touch with you

The fields marked with * are required

AI in smart manufacturing cover

AI in Smart Manufacturing: A Practical Roadmap to Data-Driven, Resilient Production

Mar 04, 2025
Imagine stepping onto a factory floor where machines anticipate their own maintenance, AI systems catch product defects invisible to the human eye, and supply chain hiccups solve themselves before you even notice a delay. This isn’t a distant dream - it’s the new baseline for competitive manufacturing operations. Welcome to the era of AI in smart manufacturing, where artificial intelligence, mach
Monika Gjorgjievska

Monika Gjorgjievska

The-Different-Types-of-Vector-Stores-and-Their-Role-in-Data-Retrieval

The Different Types of Vector Stores and Their Role in Data Retrieval

Feb 17, 2025
In the current era of technological advancements, it is evident that AI has revolutionized the operations of industrial tasks as well as day-to-day activities. One of the significant means of data retrieval for AI and machine learning applications is vector stores to which specifically, Bittensore vector store has made an extraordinary entrance to the technological field. Acting as a cutting-edge
Stefan Gligorov

Stefan Gligorov

How-Conversational-AI-Business-Intelligence-is-Revolutionizing?

How Conversational AI Business Intelligence is Revolutionizing?

Feb 10, 2025
Nowadays, the global business landscape has inevitably been influenced by conversational Artificial Intelligence (AI) to which we have to learn how to keep up with its pace. The accessibility of conversational AI Business Intelligence has demonstrated its capability to provide the luxury of [real-time insights](https://www.aimtechnologies.co/real-time-insights-the-game-changer-for-modern-businesse
Stefan Gligorov

Stefan Gligorov

View all posts