Constructors

Learn about Kotlin constructors, including primary and secondary constructors, property initialization, and default values.

When we create an object, we often want to initialize it with specific values. This is what we use constructors for. As we’ve seen already, when no constructors are specified, an empty default constructor is generated with no parameters.

Press + to interact
// Simplest class definition
class A
@Suppress("UNUSED_VARIABLE")
fun main() {
// Object creation from a class
val a: A = A()
}

To specify our custom constructor, the classic way is to use the constructor keyword inside the class body and then define its ...