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
hudsonb
09/11/2018, 1:26 PM
If you mark the property as private you will not be able to access it outside of the data class
👍 1
hudsonb
09/11/2018, 1:29 PM
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
stef
09/11/2018, 2:25 PM
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
Ahmed Mamdouh
09/12/2018, 12:31 PM
@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
hudsonb
09/12/2018, 12:32 PM
Yep!
👍 1
a
Ahmed Mamdouh
09/12/2018, 12:59 PM
@hudsonb Thank you so much , i decompiled the generated bytecode and it's great to finally see what is happening.