Deep diving into Data class Kotlin

Kishan Maurya
MindOrks
Published in
3 min readOct 25, 2019

--

Many of us already used it or some might be using it. Let’s explore more about data class in Kotlin.

What is a data class?

Data class is used to hold data/state and contains standard functionality.
In Kotlin, data keyword is used to declare a class as a data class.

How to create data classes in Kotlin?

  1. You need to append the class with the keyword data
    data class Person(val name:String)
  2. The primary constructor needs to have at least one parameter.
    data class Person() this is invalid declaration of data class
  3. Each parameter of the primary constructor must have val/var assigned.
    data class Person(name:String) WRONG
    data class Person(val name:String) CORRECT
  4. Modifiers abstract, open, sealed or inner are incompatible with data
  5. Data class can extend other classes, also can implement interfaces.

Model classes in java v/s data class in Kotlin

let's take an example of Person class

Declaration:
In Java

In Kotlin
data class Person(val name:String, var age:Int)

Initialization :

Java
Person person = new Person("demo",23);
Kotlin
var person: Person = Person("demo",23)

Usage:

Java
person.setName("Demo1"); person.getName();
person.setAge(24); person.getAge();
Kotlin
peron.name & person.age

When we declare a data class, the compiler automatically generates several functions such as toString(), equals(), hashcode() etc behind the scenes

Variable Visibility :

data class Person(private val name:String, var age:Int) 
//name variable will not be accessible outside this data class

Control over getter/setter:
Variable declared as val type can not be reassigned values. No setter will be created for variable declare as val.

data class Person(val name:String, var age:Int)
var p1 = Person("demo", 23)
p1.name = "demo1" //val can't be reassigned
p1.age = 23 //this is valid

Kotlin also generates a copy() function and componentN() functions for all the data classes.

Copy():
It is used to create a copy of an instance of the data class with a few of the properties modified.
If we want to create a copy of a person(p1) by changing the age only,

val p1= Person("Test", 56)
val p1Copy= p1.copy(age = 63)
print(p1.toString()) //Person(name=Test, age=56)
print(p1Copy) // Person(name=Test, age=63)

Destructuring Declarations: The componentN() functions
componentN() function lets us access each of the arguments specified in the primary constructor, in the order specified. N is the number of parameters in the constructor.

val person : Person = Person( "Article", 24)
println(person.component1()) //Article
println(person.component2()) //24

The component functions enable us to use the so-called Destructuring Declaration in Kotlin. The Destructuring declaration syntax helps you destructure an object into a number of variables

val person : Person = Person( "Article", 24)
//Destructuring Declaration
val (personName, personAge) = person
println("name = $personName, age = $personAge")
//name = Article, age = 24

But if any of variable declared as private then destructing is not allowed.

data class Person(private val name:String, var age:Int)
var p1 = Person("demo",24)
var( name, age ) = p1 //this is not allowed

Some Standard Data class in Kotlin
Pair:

var myPair = Pair<String,String>("Demo","data class")
print(myPair.first)
print(myPair.second)

Triple:

var myTriple = Triple<String,String, Int>("Demo","data class",24)
print(myTriple.first)
print(myTriple.second)
print(myTriple.third)

Please visit the Data Classes and Destructuring Declarations pages in the official Kotlin Documentation.

Feel free to share some good real example of how you use data classes as a response to this blog.

You could check out my other interesting topics here.

--

--