class kotlass(var propOne:String, var propTwo:String) {...}
So this is the concise way of declaring properties in Kotlin. I want to change setters (default implementation) to private. Is this possible without giving away the concise syntax?
class kotlass(var propOne:String, var propTwo:String) {...}
So this is the concise way of declaring properties in Kotlin. I want to change setters (default implementation) to private. Is this possible without giving away the concise syntax?
You can use the val
keyword instead of var
. val
stands for value
, and means the property will be immutable and only a getter will be generated (var
stants for variable
and generates both a getter and a setter).
See also the official reference:
Much the same way as regular properties, the properties declared in the primary constructor can be mutable (var) or read-only (val).
This doesn't strictly "change the setter to private". It actually causes the property to be immutable. If you want the variable to be mutable internally, then no, there is no way to do that using only the default constructor. You would have to use methods described in Kotlin : Public get private set var.