A constructor is the setup pattern that lets you feed initial data into a class when you create an object. In Kotlin, you can write the primary constructor right after the class name, which keeps the code short and clear.
kotlin
class Person(val firstName: String, var age: Int)
fun main() {
val person = Person("John", 35)
println("${person.firstName} is ${person.age} years old.")
}You should see
John is 35 years old.Summary
A constructor is essential for feeding correct data into an object right when it's created.