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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -