What is the correct way to define a var in kotlin that has a public getter and private (only internally modifiable) setter?
var setterVisibility: String = "abc" // Initializer required, not a nullable type
private set // the setter is private and has the default implementation
See: Properties Getter and Setter
However, in case of constructor(s), if possible use val
instead of var
, otherwise, Kotlin constructors do NOT support "private set", and you would need to create separate property, and set that through constructor:
class MyClass(private var myConstructorArg: String) {
var myPropery: String = myConstructorArg; private set
}