A Kotlin constructor initializes a new instance and receives the arguments needed to create it. The primary constructor usually provides the concise path: its parameters can initialize properties directly, avoiding repetitive field assignments and getters. section 4.2.1 section 2.2
A primary constructor with a property
class User( val nickname: String, val isSubscribed: Boolean = true ) fun main() { val alice = User("Alice") val bob = User("Bob", false) val carol = User("Carol", isSubscribed = false) } `val` promotes a constructor parameter to a property. Default and named arguments make common construction concise while preserving explicit alternatives. section 4.2.1
Use an initializer block when construction requires executable logic that the primary-constructor syntax cannot contain. Initialization code runs when the instance is created, and a class may contain several initializer blocks. section 4.2.1
Choose the constructor form that keeps initialization explicit and maintainable:
- Write `constructor` explicitly when the primary constructor has annotations or visibility modifiers; otherwise, omit the keyword. section 4.2.1
- Use a secondary constructor inside the class body when several construction paths are required. section 4.2
- Pass constructor arguments to a superclass, as in `class SocialUser(nickname: String) : User(nickname)`. section 4.2.1
- Restrict construction with a private constructor when instances should not be created freely. section 4.2.1
Initialization versus computed access
class SocialUser(val accountId: Int) : User { override val nickname = getNameFromSocialNetwork(accountId) } The social-network lookup runs during initialization and its result is stored in a backing field. This differs from a custom getter, which recalculates a value whenever the property is accessed. section 4.2.3
class User(val nickname: String)
See the smallest idiomatic form: a primary-constructor parameter declared directly as a property. section 4.2.1








