Should properties of class in Kotlin be private and how to access them? -
aloha! while reading kotling language reference noticed keyword "private" properties of class never used (always default public). said getters , setters generated automatically. created class , made fields private. however, when create object of class, cannot see fields , no setters , getters available, unless write them myself. what's rule here? leave visibility modifier default (public) or make them private , provide mutator methods? thank you.
the whole idea of property encapsulates field , accessors in single entity. if need able access , modify property of class outside, should keep property public. if need able read outside not update it, can define public property private accessor.
changing default accessor custom 1 not affect clients of class, because under hood compiler generate accessor methods, , clients of class use methods , not access underlying field directly.
you should never write explicit getter or mutator methods separate property accessors.
Comments
Post a Comment