October 2019
Intermediate to advanced
434 pages
11h 54m
English
If a property is defined inline within the primary constructor, then its value will be assigned before any other initialization occurs. In this case, the property initialization will be placed at the beginning of the compiler-generated constructor.
In the following code, notice we have two primary constructor parameters—though only lastName is defined as a property:
class Student(_firstName: String, val lastName: String) { val firstName = _firstName val id: String var nickname = "" init { id = generateStudentId(firstName, lastName) } val subjects:MutableList<String> = mutableListOf()}
Even though lastName is not the first parameter defined in the primary constructor, it will be the first property to be initialized ...
Read now
Unlock full access