August 2018
Beginner
178 pages
4h 56m
English
There are also secondary constructors; the syntax is similar to primary constructors. The difference is that secondary constructors are declared inside the class body:
class User { constructor(firstName: String, lastName: String, birthYear: Int) { }}
Secondary constructors cannot have properties, only constructor parameters. If you need to initialize your fields with constructor parameters, you can do that inside the init block.
You can also have both primary and secondary constructors inside the same class. In that case, the secondary constructor has to call the primary one. This is done with the colon and the this keyword after the secondary constructor declaration:
class User(firstName: String, lastName: String
Read now
Unlock full access