apply and also
Configure and side-effect.
apply and also 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.
Configuring Objects
apply and also are scope functions that return the object itself rather than the lambda result. This makes them perfect for configuration and chaining.
The apply Function
apply runs a block with the object as this and returns the object. It is the classic tool for initializing an object after creation.
fun main() {
val builder = StringBuilder().apply {
append("Hello")
append(", ")
append("World")
}
println(builder.toString())
}Why apply Returns the Object
Because apply returns the receiver, you can assign the configured object directly to a variable.
data class Person(var name: String = "", var age: Int = 0)
fun main() {
val p = Person().apply {
name = "Alice"
age = 30
}
println(p)
}apply Uses this
Inside apply, the object is the receiver, so you access its properties directly without any prefix.
data class Config(var host: String = "", var port: Int = 0)
fun main() {
val config = Config().apply {
host = "localhost"
port = 8080
}
println("${config.host}:${config.port}")
}The also Function
also runs a block with the object as it and returns the object. It is used for side effects such as logging or validation that do not change the object.
fun main() {
val numbers = mutableListOf(1, 2, 3)
numbers
.also { println("Before: $it") }
.add(4)
println("After: $numbers")
}also Uses it
Because also exposes the object as it, it is convenient when you still want to refer to the outer this or just keep the reference explicit.
fun main() {
val value = 42
.also { println("Logging value: $it") }
println(value)
}apply vs also
The difference mirrors run vs let:
applyusesthisand is best for configuring properties.alsousesitand is best for side effects on the object.
Both return the original object.
data class Box(var size: Int = 0)
fun main() {
val box = Box()
.apply { size = 5 }
.also { println("Created box with size ${it.size}") }
println(box)
}Chaining apply and also
Since both return the object, you can chain them to first configure and then perform side effects in a fluent pipeline.
data class Account(var owner: String = "", var balance: Int = 0)
fun main() {
val account = Account()
.apply { owner = "Sam" }
.apply { balance = 100 }
.also { println("New account: $it") }
println(account.balance)
}also for Validation
Use also to insert a check in the middle of a chain without breaking the flow.
fun main() {
val result = listOf(1, 2, 3, 4)
.filter { it % 2 == 0 }
.also { check(it.isNotEmpty()) { "No even numbers" } }
println(result)
}Returning the Object Enables Pipelines
The shared trait of apply and also is that they never interrupt a chain, because they always hand back the same object you started with.
fun main() {
val text = StringBuilder()
.apply { append("a") }
.also { it.append("b") }
.apply { append("c") }
println(text)
}Choosing apply or also
Reach for apply when initializing or mutating an object using its own members. Reach for also when performing an action that uses the object but does not configure it, like logging, debugging, or validation.
Quick Check
Which value do apply and also return?
Recap
You learned the two object-returning scope functions:
applyusesthisfor configuring an object.alsousesitfor side effects on an object.
Both return the object, enabling clean chains. Next you will group operations with with.
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 “apply and also” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “apply and also”, 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 “apply and also”?
Configure and side-effect. 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 “apply and also” 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
- let and run
- apply and also
- with
- Choosing the Right One