Kotlin Academy · Lesson

Copying with copy() and Partial Overrides

Use copy() to create modified instances without mutating the original.

Lesson 2 of 413 steps

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()

copy() creates a new instance of a data class with some properties modified. All unspecified properties retain their original values.

Basic copy()

Pass named arguments to override specific properties.
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

Override as many fields as you need.
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

copy always creates a new object. The original is completely unchanged.
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

copy is shallow. Mutable objects inside a data class are shared between original and copy.
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

Use immutable data inside data classes to avoid sharing 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

copy is the idiomatic way to update state in Redux-style or MVI architectures.
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 classes with many properties still have ergonomic copy thanks to named 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)  // Hello

Using copy in Tests

copy makes test fixtures easy — create one base object and vary just the fields you need.
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

Data class copy() is Kotlin's answer to the Builder pattern. For complex objects with many optional fields, data class + copy is more concise.
// Java Builder:
User.builder().name("A").age(30).role("admin").build()
// Kotlin:
val u = User("A", 30, "admin")  // or copy from base

Record Semantics

Data classes with val properties behave like Java records (JDK 16+) — immutable, value-based, automatically printable.
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

What happens to the original data class instance when you call copy() on it?

Recap

copy() creates a new instance with overridden fields — the original is unchanged. Great for state management, tests, and builder-style construction. Beware of shallow copies with mutable properties. Next: destructuring!
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 “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

  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