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. This is lesson 2 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.
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
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 “Copying with copy() and Partial Overrides” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Copying with copy() and Partial Overrides”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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