Kotlin Data Class

Kotlin Data Class is used when the class is intended to represent a complex model that contains only properties(maybe belonging to different data types) and object of the class is required to hold only the properties’ values(data). Classes that hold only data, hence Data Classes.

Also, Data classes are useful when a function has to return more than one value. All the values that are required to be returned are encapsulated in a data class and the object of data class is returned.

The structure of data class is similar to that of a usual Kotlin Class, except that the keyword data precedes the keyword class.

Example 1 – Kotlin Data Class

In the below example we shall define a data class “Book” with variables “name” and “price“. And add objects of Book to an array of Book, and finally print them.

example.kt

import java.util.ArrayList

fun main(args: Array) {
    var books: ArrayList = ArrayList()
    books.add(Book("The Last Sun",250))
    books.add(Book("Three Friends",221))
    for(book in books)
        println(book)
}

data class Book(val name: String = "", val price: Int = 0)

The default values (name=””, price=0) are provided in the definition of data class.

Output

Book(name=The Last Sun, price=250)
Book(name=Three Friends, price=221)
ADVERTISEMENT

Modify & Copy a Data Class

Kotlin allows copying an existing data class object to a new variable as is or with modifications to some of the parameters of Data Class.

The following example demonstrates how to copy a data class object to other; modify and copy a data class object to other.

example.kt

import java.util.ArrayList

fun main(args: Array) {
    var books: ArrayList = ArrayList()
    var book1 = Book("The Last Sun",250)
    var book2 = Book("Three Friends",222)
    var book3 = book1.copy()
    var book4 = book2.copy(name="The Last Bond")
    var book5 = book2.copy(price=263)
    books.add(book1)
    books.add(book2)
    books.add(book3)
    books.add(book4)
    books.add(book5)
    
    for(book in books)
    	println(book)
}

data class Book(val name: String = "", val price: Int = 0)
  • book1, book2 are new objects created.
  • book3 is a copy of book1.
  • book4 is a copy of book2 with name modified to “The Last Bond”.
  • book5 is a copy of book2 with price modified to 263.

Output

Book(name=The Last Sun, price=250)
Book(name=Three Friends, price=222)
Book(name=The Last Sun, price=250)
Book(name=The Last Bond, price=222)
Book(name=Three Friends, price=263)

Component Functions

Component functions are used to access individual properties of a data class. An example is provided below, where we access the properties, “name” and “price” of data class “Book” using component functions.

example.kt

fun main(args: Array) {
    var book1 = Book("The Last Sun",250)
    var name = book1.component1();
    var price = book1.component2();
    println("$name has a price of $$price")
}

data class Book(val name: String = "", val price: Int = 0)

Output

The Last Sun has a price of $250

Conclusion

In this Kotlin TutorialKotlin Data Class, we have learnt what a Kotlin Data Class is, how to initialize an object of data class, how to copy one data class object to other with some properties(or you can say parameters) modified, and also to access the individual parameters of Kotlin data class using component functions.