I have a question about visibility of fields in da...
# announcements
a
I have a question about visibility of fields in data classes , i want it to be private but not sure how to set and get the value , putting var before field parameters can ensure that it has getters and setters but what if i put private before the fields in the parameter? Should i write a getters and setters myself ?
h
If you mark the property as private you will not be able to access it outside of the data class
👍 1
You say you want it private but want to be able to get/set the value. There's a backing field that's effectively "private", the property provides get/set access to that backing field, You wouldn't write get/set yourself.
👍 1
s
I thought that even when you access the field directly that kotlin under the hood is calling default getter/setter and so essentially all fields are private with public get/set
👍 2
a
@hudsonb thank you but just to make sure i understand this , i would only use private if i don't want my fields to be accessed outside that class at all ? But otherwise that backing field is private and has getters and setters by default ?
h
Yep!
👍 1
a
@hudsonb Thank you so much , i decompiled the generated bytecode and it's great to finally see what is happening.