Declaring Data Classes
Concise model classes.
Declaring Data Classes is a free Kotlin 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 Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Classes for Holding Data
Many classes exist only to bundle a few values together: a user, a point, a result.
Kotlin gives you the data class, a concise way to declare such model classes with lots of free behavior.
Declaring a Data Class
Add the data keyword before class and list the properties in the constructor.
That single line gives you a fully featured model.
data class User(val name: String, val age: Int)
fun main() {
val u = User("Alice", 30)
println(u.name)
println(u.age)
}Free toString
A data class auto-generates a readable toString(). Printing the object shows its class name and property values.
Compare this to a regular class which would print a cryptic hash.
data class User(val name: String, val age: Int)
fun main() {
val u = User("Bob", 25)
println(u)
}Properties Are Required
A data class must declare at least one property in its primary constructor.
Each parameter should be val or var so it becomes a property the generated functions can use.
data class Point(val x: Int, val y: Int)
fun main() {
val p = Point(3, 4)
println(p)
}val vs var Properties
Use val for read-only properties and var for mutable ones.
Immutable data classes are usually preferred because they are easier to reason about.
data class Counter(var count: Int)
fun main() {
val c = Counter(0)
c.count = 5
println(c)
}Default Parameter Values
Like any constructor, a data class can have default values.
This lets callers omit parameters they do not need to set.
data class User(val name: String, val active: Boolean = true)
fun main() {
val u = User("Mia")
println(u)
}Named Arguments
Combine data classes with named arguments for clear, self-documenting construction.
This is especially helpful when several properties share the same type.
data class Rect(val width: Int, val height: Int)
fun main() {
val r = Rect(width = 10, height = 5)
println(r)
}Adding Methods
A data class can still have a body with its own functions and computed properties.
The generated functions live alongside whatever you add.
data class Circle(val radius: Double) {
fun area(): Double = 3.14159 * radius * radius
}
fun main() {
val c = Circle(2.0)
println(c.area())
}Properties Outside the Constructor
Only properties in the primary constructor are used by the generated functions.
A property declared in the body is part of the object but ignored by toString, equals, and friends.
data class User(val name: String) {
var nickname: String = ""
}
fun main() {
val u = User("Alex")
u.nickname = "Al"
println(u) // nickname not shown
println(u.nickname)
}When to Use Data Classes
Reach for a data class when an object mainly carries values:
- DTOs and API models
- Coordinates, ranges, results
- Configuration bundles
Avoid them for classes built around complex behavior or inheritance.
Putting It Together
A data class is a compact model declaration:
- Prefix with
dataand list properties - Get
toString,equals,hashCode,copyfor free - Supports defaults, named args, and extra methods
data class Book(val title: String, val pages: Int = 0)
fun main() {
val b = Book(title = "Kotlin", pages = 300)
println(b)
}Quick Check
Test your understanding of declaring data classes.
Recap
You learned how to declare data classes:
- The
datakeyword generates standard functions - Constructor properties drive the generated behavior
- Defaults and named arguments make them flexible
Next you will explore the generated copy and equals functions in depth.
Frequently asked questions
Is the “Declaring Data Classes” lesson free?
Yes — the full text of “Declaring Data Classes” is free to read here on the web, and the Kotlin 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 Academy course, upgrade to CoddyKit PRO.
What will I learn in “Declaring Data Classes”?
Concise model classes. You practise Kotlin 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 Academy?
No prior experience is required. Kotlin 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 “Declaring 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 Kotlin Academy lesson?
Yes. Every Kotlin 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
- Declaring Data Classes
- copy and equals
- Destructuring Declarations
- Data Classes in Collections