🐦 Kotlin
Beginner
How is a primary constructor defined in Kotlin?
Answer
The primary constructor is declared directly in the class header: class Person(val name: String, val age: Int). Properties declared with val or var in the constructor are automatically created — no need for field declarations and assignments in the body. Initialization logic goes in init { } blocks, which run as part of the primary constructor. If the constructor needs a visibility modifier or an annotation, add the constructor keyword: class Person private constructor(val name: String).