Menu

Kotlin Data Class vs Regular Class Explained Simply
📰

Kotlin Data Class vs Regular Class Explained Simply

DEV Community: kotlin·Mayank Kumar·1 day ago
#EPGAEJkj
#devtokotlin#devto#dev#class#copy
Reading 0:00
15s threshold

In short: a data class is a normal class where the compiler writes five things for you: equals() , hashCode() , toString() , copy() and component1() , component2() and so on. It uses only the properties in the primary constructor, and copy() copies values, not the objects inside them.

You add an item to the cart, the ViewModel updates its StateFlow , and the screen does not change. No crash, nothing in Logcat. The state is a data class and the update uses copy() . The bug is in how copy() and equals() work together.

This post covers what the data keyword adds, the rules it comes with, and the two details behind surprising data class bugs.

What is a data class in Kotlin?

A data class is a class whose main job is to hold values.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.