What is difference between object and data class in Kotlin?

object

object is Kotlin's way to create a singleton (one instance class) which is instantiated by the compiler.


data class

A data class is like a usual class but with a few advantages/resctrictions (Source).

Advantages

  • equals()/hashCode()
  • toString()
  • componentN()
  • copy()

Those are created from the properties specified in the primary constructor.

Restrictions

  • The primary constructor needs to have at least one parameter;
  • All primary constructor parameters need to be marked as val or var;
  • cannot be abstract, open, sealed or inner;
  • (before 1.1) may only implement interfaces.

First, there is no object class, the feature you are referring to is called object declaration.

Object declaration

This is a feature in Kotlin that allows you implement a singleton. The object declaration combines a class declaration and a declaration of a single instance of the class in a single statement.

// Let's assume that class Person is defined somewhere else
object Payroll {
    val employees = arrayListOf<Person>()

    fun calculateSalary() {
        for (person in employees) {
            // ...
        }
    }
}

// calling methods and properties
>>> Payroll.employees.add(Person("John", 23)) // calling a property
>>> Payroll.calculateSalary() // calling a method

Just like a class, an object declaration can contain declarations of properties, methods, initializer blocks, and so on. The only thing they are not allowed are constructors (either primary or secondary).

Object declarations are created immediately at the point of the definition, not through constructor calls from other places in the code.

Note: the object keyword can also be used for companion objects and object expressions.

Data Class

It is very common to create classes whose main goal is to hold data. If you want your class to be a convenient holder for your data you need to override the universal object methods:

  • toString() - string representation
  • equals() - object equality
  • hashCode() - hash containers

However, by adding the modifier data to your class, the necessary methods are automatically added for you. In addition, the following methods are also generated:

  • componentN() functions corresponding to the properties in their order of declaration
  • copy() function
class PersonClass(val name: String, val age: Int) // regular class
data class PersonDataClass(val name: String, val age: Int) // data class

In summary, if you need a holder for data, you should use a data class which means adding the modifier data to your class. This will generate the following methods for you: toString(), equals(), hashCode(), componentN(), and copy(), so you avoid writing boilerplate code. On the other hand, if you need to create a singleton, you use the object declaration feature.


Kotlin's object is similar to a class in Java, where all methods and variables are static.

object User {
    val name = ""
    fun printName(name: String) = "Hello, $name!"
}

in Kotlin is similar to the following in Java:

class User {
    public static String name = "";
    public static String printName(name: String) {
        return "Hello " + name + "!";
    }
}

Usage example:

//Kotlin
User.printName(User.name)

//Java
User.printName(User.name);

An object isn't exactly the same as the Java comparison I gave, though. It can inherit interfaces and classes, and the object itself is instantiated as a singleton instance. If you annotate methods inside an object with @JvmStatic, they will become true static members.

Kotlin's object


The data class in Kotlin is just a simpler syntax for a class that has no (or minimal) logic, and contains certain values. Kotlin generates the equals(), hashCode() and toString() functions for you in a data class, along with some other helper functions.

data class User(val name: String, val age: String)

in Kotlin will look something like this in Java:

class User {
    public final String name;
    public final String age;

    public User(String name, String age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object other) {
        //Kotlin-generated equality check
    }

    @Override
    public long hashCode() {
        //Kotlin's hashcode
    }

    @Override 
    public String toString() {
        return "User(name=" + name + ",age=" + age + ")";
    }

    //other generated methods
}

Kotlin's data class documentation

Tags:

Kotlin