A constructor parameter supplies values when a Kotlin object is created. Prefixing it with val or var also promotes it to a property, keeping simple class declarations compact. section 4.2.1
class User(val nickname: String)
This compact declaration accepts a nickname and exposes it as a read-only property. section 4.2.1
Default and named arguments
class User( val nickname: String, val isSubscribed: Boolean = true ) User("Alice") User("Bob", false) User("Carol", isSubscribed = false) A parameter can therefore provide a default value, accept positional input, or be selected by name. section 4.2.1
Use an explicit form when initialization needs more structure:
- Declare a parameter without val or var when it only supplies initialization data. section 4.2.1
- Refer to primary-constructor parameters from property initializers and initializer blocks. section 4.2.1
- Pass constructor parameters to a superclass when declaring a subclass. section 4.2.1
