Quantcast
Viewing latest article 3
Browse Latest Browse All 5

Answer by s1m0nw1 for Kotlin: 'val' on secondary constructor parameter is not allowed

In addition to the great answer of yole, the documentation is pretty clear as well:

Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body. [...] In fact, for declaring properties and initializing them from the primary constructor, Kotlin has a concise syntax:

class Person(val firstName: String, val lastName: String, var age: Int) {    // ...}

Much the same way as regular properties, the properties declared in the primary constructor can be mutable (var) or read-only (val).

This all does not apply to secondary constructors.


Viewing latest article 3
Browse Latest Browse All 5

Trending Articles