Data Classes
Concise classes for holding data.
Data Classes is a free Android Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Data Class?
A data class is a class made to hold data. Kotlin generates useful methods for it automatically.
You declare one by writing data before class.
Declaring a Data Class
A data class needs at least one parameter in its primary constructor, each marked val or var.
Here is a User that stores a name and an age.
data class User(val name: String, val age: Int)
fun main() {
val u = User("Ada", 30)
println(u.name)
}A Readable toString
A normal class prints a cryptic address. A data class generates a clear toString showing each property.
This makes debugging much easier.
data class User(val name: String, val age: Int)
fun main() {
val u = User("Ada", 30)
println(u)
}Comparing with equals
Data classes compare by content, not identity. Two objects with the same property values are equal with ==.
A regular class would say they are different.
data class Point(val x: Int, val y: Int)
fun main() {
val a = Point(1, 2)
val b = Point(1, 2)
println(a == b)
}The copy Function
The generated copy function makes a new object based on an existing one. You can change just the properties you name.
The original stays untouched.
data class User(val name: String, val age: Int)
fun main() {
val u = User("Ada", 30)
val older = u.copy(age = 31)
println(older)
}Destructuring
Data classes support destructuring: unpacking properties into separate variables in one line.
The order follows the constructor.
data class User(val name: String, val age: Int)
fun main() {
val (name, age) = User("Sam", 25)
println(name + " " + age)
}Default Values Work Too
Like any constructor, data class parameters can have default values.
This lets callers omit fields they don't need.
data class User(val name: String, val active: Boolean = true)
fun main() {
val u = User("Lee")
println(u)
}hashCode for Collections
Data classes also generate hashCode, so they behave correctly as keys in a Set or Map.
Equal objects produce the same hash, so duplicates are detected.
data class Point(val x: Int, val y: Int)
fun main() {
val set = setOf(Point(1, 1), Point(1, 1))
println(set.size)
}When to Use Data Classes
Reach for a data class when an object mainly carries values rather than complex behavior.
- API responses
- UI state in Android
- Database records
For objects rich in logic, a regular class is the better fit.
Data Classes Save Work
A single data keyword gives you toString, equals, hashCode, copy, and destructuring.
That is a lot of boilerplate you no longer have to write or maintain by hand.
Data Class vs Regular Class
A regular class compares by identity and prints a memory reference. A data class compares by content and prints its values.
The data keyword is what unlocks all of these generated behaviors.
class Plain(val x: Int)
data class Smart(val x: Int)
fun main() {
println(Plain(1) == Plain(1))
println(Smart(1) == Smart(1))
}Quick Check
Test your understanding of data classes.
Recap
You learned that a data class auto-generates toString, equals, hashCode, copy, and destructuring support.
They are ideal for value-carrying objects like UI state and API models. You have now covered functions, classes, constructors, and data classes in Kotlin.
Frequently asked questions
Is the “Data Classes” lesson free?
Yes — the full text of “Data Classes” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Data Classes”?
Concise classes for holding data. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Data Classes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.