Are public attributes rather than private attributes with get/set methods common practice?

And I would like to add a nice flavor to property mechanism:

class MyClass {
   var variableReadOnlyFromOutside : String = ""
      private set

   fun someFunctionAlteringField() { 
      variableReadOnlyFromOutside = "set"
   }
}


val myClass : MyClass()
println(myClass.variableReadOnlyFromOutside)           // Valid
myClass.variableReadOnlyFromOutside = "not allowed"    // Invalid: cause compile time error

So one can easily make a externally read-only, but internally modifiable property.

2 Likes