Data Classes for Your Domain
Immutable shared models with copy and equals for free.
Data Classes for Your Domain is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 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 Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Your Domain Needs Models
Every app describes real things like users, orders or songs. In KMP you model them once with shared data classes.
Meet the data class
A data class is a small type built to hold values. You add the keyword and list the properties in the header.
data class User(val id: Int, val name: String)It Lives in commonMain
Put your model in commonMain and both the Android and iOS apps see the exact same shape with no duplication.
// shared/src/commonMain
data class User(val id: Int, val name: String)Create an Instance
Build a model by calling it like a function and passing each value. The object now carries your data everywhere.
val ada = User(id = 1, name = "Ada")Read the Properties
Access any field with a dot. Reading properties works the same in shared code, Android and iOS.
val label = ada.name + " #" + ada.idFree toString
A data class auto-generates a readable toString, so printing a model shows its values instead of a cryptic address.
println(ada) // User(id=1, name=Ada)Value-Based equals
Two models with the same fields are considered equal. Data classes compare by content, not by memory identity.
User(1, "Ada") == User(1, "Ada") // trueCopy with Changes
The free copy function clones a model while overriding only the fields you name, keeping the original untouched.
val renamed = ada.copy(name = "Grace")Prefer Immutable val
Declare fields as val so models cannot change after creation. Immutable data is safer to share across platforms.
data class Song(val title: String, val seconds: Int)Compose Models Together
A model can hold other models as fields, letting you nest data classes to mirror real relationships in your domain.
data class Playlist(val owner: User, val songs: List<Song>)Why This Matters
One shared model means Android and iOS can never disagree about your data shape, killing a whole class of bugs. 🙂
Quick Check
Which free feature did the data class give you here?
Recap
Shared data classes in commonMain give you copy, equals and toString for free, modeling your domain once for both apps.
Frequently asked questions
Is the “Data Classes for Your Domain” lesson free?
Yes — the full text of “Data Classes for Your Domain” is free to read here on the web, and the Kotlin Multiplatform 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 Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Data Classes for Your Domain”?
Immutable shared models with copy and equals for free. You practise Kotlin Multiplatform 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 Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Data Classes for Your Domain” 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 Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform 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.
All lessons in this course
- Data Classes for Your Domain
- Enums & Sealed Classes for State
- Null Safety in Shared Models
- Validation Helpers on Your Models