Primary Constructor (Kotlin)
The primary constructor is the concise constructor declared in a class's header, right after its name. Prefixing its parameters with val or var turns them into properties in one line.
Kotlin
class Person(val name: String, var age: Int) declares the class and its constructor together. Marking a parameter val or var simultaneously declares a property and initializes it from the argument, eliminating the field/assignment boilerplate Java needs.
The primary constructor itself has no body; initialization logic lives in init blocks and property initializers, which run in declaration order. Parameters without val/var are constructor-only (usable in init and initializers but not stored as properties).
A class can also have secondary constructors (constructor(...) { }) that must ultimately delegate to the primary one with this(...). Default parameter values often remove the need for secondary constructors entirely.
Syntax
class Point(val x: Int = 0, val y: Int = 0)Examples
Kotlin
class Book(val title: String, val pages: Int = 100)
val b = Book("Kotlin")
println("${b.title}: ${b.pages} pages")Output:
Kotlin: 100 pages