Copying with copy() and Partial Overrides
Use copy() to create modified instances without mutating the original.
Copying with copy() and Partial Overrides is a free Kotlin Academy lesson on CoddyKit — lesson 2 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.
Immutable Updates with copy()
Basic copy()
data class Person(val name: String, val age: Int, val city: String)
val alice = Person("Alice", 30, "Berlin")
val olderAlice = alice.copy(age = 31)
println(olderAlice) // Person(name=Alice, age=31, city=Berlin)Copying Multiple Fields
data class Config(val host: String, val port: Int, val ssl: Boolean, val timeout: Int)
val prod = Config("prod.server", 443, true, 30)
val test = prod.copy(host = "test.server", port = 8080, ssl = false)
println(test)copy Doesn't Modify the Original
data class Point(val x: Int, val y: Int)
val origin = Point(0, 0)
val moved = origin.copy(x = 5)
println(origin) // Point(x=0, y=0) — unchanged
println(moved) // Point(x=5, y=0)Deep Copy Caveat
data class Team(val name: String, val members: MutableList<String>)
val t1 = Team("A", mutableListOf("Alice"))
val t2 = t1.copy(name = "B")
t2.members.add("Bob") // modifies SHARED list!
println(t1.members) // [Alice, Bob] ← also changed!Avoiding Shallow Copy Issues
data class Team(val name: String, val members: List<String>) // immutable list
val t1 = Team("A", listOf("Alice"))
val t2 = t1.copy(name = "B", members = t1.members + "Bob")
println(t1.members) // [Alice]
println(t2.members) // [Alice, Bob]copy in State Management
data class UiState(val loading: Boolean, val items: List<String>, val error: String?)
fun reduce(state: UiState, action: Action) = when (action) {
is Loading -> state.copy(loading = true)
is Success -> state.copy(loading = false, items = action.data)
is Error -> state.copy(loading = false, error = action.msg)
}copy with Default Parameters
data class Article(
val id: Long, val title: String, val body: String,
val published: Boolean = false, val views: Int = 0
)
val draft = Article(1L, "Hello", "Content...")
val published = draft.copy(published = true)
println(published.title) // HelloUsing copy in Tests
val baseUser = User(id = 1, name = "Test", email = "test@test.com", active = true)
val inactiveUser = baseUser.copy(active = false)
val renamed = baseUser.copy(name = "Renamed")copy vs Builder Pattern
// Java Builder:
User.builder().name("A").age(30).role("admin").build()
// Kotlin:
val u = User("A", 30, "admin") // or copy from baseRecord Semantics
data class RGB(val r: Int, val g: Int, val b: Int)
val white = RGB(255, 255, 255)
println(white) // RGB(r=255, g=255, b=255)Quick Check
Recap
Frequently asked questions
Is the “Copying with copy() and Partial Overrides” lesson free?
Yes — the full text of “Copying with copy() and Partial Overrides” 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 “Copying with copy() and Partial Overrides”?
Use copy() to create modified instances without mutating the original. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Copying with copy() and Partial Overrides” 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
- Data Class Basics: Auto-generated Methods
- Copying with copy() and Partial Overrides
- Destructuring Declarations with componentN
- Data Classes in Collections and as Map Keys