Kotlin Academy · Lesson

Data Class Basics: Auto-generated Methods

Understand what data class generates: equals, hashCode, toString, copy.

Lesson 1 of 413 steps

Data Class Basics: Auto-generated Methods is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

What Is a Data Class?

A data class is a class whose primary purpose is holding values. The Kotlin compiler auto-generates equals, hashCode, toString, copy, and componentN functions for you.

Declaring a Data Class

Prefix a regular class with data. All primary constructor parameters must be val or var.

data class User(val name: String, val age: Int)
fun main() {
    val u = User("Ada", 35)
    println(u)
}

Auto-generated toString

The compiler creates a toString that lists every property by name.

data class Point(val x: Int, val y: Int)
fun main() {
    println(Point(3, 5)) // Point(x=3, y=5)
}

Auto-generated equals

equals compares all property values — two instances with the same fields are equal regardless of reference identity.

data class Coord(val lat: Double, val lng: Double)
fun main() {
    val a = Coord(41.0, 29.0)
    val b = Coord(41.0, 29.0)
    println(a == b)      // true (structural)
    println(a === b)     // false (reference)
}

Auto-generated hashCode

hashCode is consistent with equals: equal objects produce equal hashes, so data classes work correctly in sets and as map keys.

data class Key(val id: Int, val tag: String)
fun main() {
    val map = mapOf(Key(1, "a") to "one")
    println(map[Key(1, "a")]) // one
}

Auto-generated copy

copy() creates a new instance with the same field values, allowing per-field overrides via named arguments.

data class Config(val host: String, val port: Int, val tls: Boolean)
fun main() {
    val dev = Config("localhost", 8080, false)
    val prod = dev.copy(host = "prod.example.com", tls = true)
    println(prod) // Config(host=prod.example.com, port=8080, tls=true)
}

Auto-generated componentN

Each property gets a componentN() function (component1, component2, ...) enabling destructuring.

data class Pair2(val first: String, val second: Int)
fun main() {
    val p = Pair2("Ada", 35)
    val (name, age) = p
    println("$name is $age")
}

What Data Classes Don't Do

Data classes do not generate toString for inherited fields, are final by default, and cannot be abstract, open, sealed, or inner.

data class Sample(val n: Int)
// open data class Bad(...) // ERROR
// sealed data class Bad(...) // ERROR
fun main() { println(Sample(1)) }

Properties Outside Primary Constructor

Properties declared in the class body do NOT participate in equals, hashCode, toString, or copy.

data class Person(val name: String) {
    var nickname: String = ""
}
fun main() {
    val a = Person("Ada").apply { nickname = "Augusta" }
    val b = Person("Ada").apply { nickname = "Different" }
    println(a == b)   // true — only name matters
    println(a)         // Person(name=Ada)
}

Default Values

Combine data class with default parameters for flexible construction.

data class Request(
    val url: String,
    val method: String = "GET",
    val headers: Map<String, String> = emptyMap()
)
fun main() {
    println(Request("https://api.example.com"))
}

Practical: DTO Class

Data classes are perfect for DTOs, API responses, and value objects.

data class ApiResponse(
    val status: Int,
    val body: String,
    val timestamp: Long
)
fun main() {
    val ok = ApiResponse(200, "{}", System.currentTimeMillis())
    println(ok)
}

Quick Check

Which method is NOT auto-generated by the Kotlin compiler for a data class?

Recap

A data class auto-generates equals, hashCode, toString, copy, and componentN from its primary constructor properties. Properties outside the constructor are excluded. Use data classes for DTOs, value objects, and any container of related values.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Data Class Basics: Auto-generated Methods” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Data Class Basics: Auto-generated Methods”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “Data Class Basics: Auto-generated Methods”?

Understand what data class generates: equals, hashCode, toString, copy. 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Data Class Basics: Auto-generated Methods” 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

  1. Data Class Basics: Auto-generated Methods
  2. Copying with copy() and Partial Overrides
  3. Destructuring Declarations with componentN
  4. Data Classes in Collections and as Map Keys
← Back to Kotlin Academy