Is there any way I can make a private setter and a public getter in a Kotlin Data Class?
data class Test(var attribute: String) {
// attribute can be mutated inside this class
// but outside only readable ?
}
In programming, getters are used for getting value of the property. Similarly, setters are used for setting value of the property. In Kotlin, getters and setters are optional and are auto-generated if you do not create them in your program.
Usually you want setters/getters to be public, because that's what they are for: giving access to data, you don't want to give others direct access to because you don't want them to mess with your implementation dependent details - that's what encapsulation is about.
In Kotlin, setter is used to set the value of any variable and getter is used to get the value. Getters and Setters are auto-generated in the code. Let's define a property 'name', in a class, 'Company'. The data type of 'name' is String and we shall initialize it with some default value.
In general, they should be public. If they are private they can only be called from within your class and, since you already have access to the private variables within your class, are redundant. The point of them is to allow access to these variables to other, outside, objects.
Kotlin | Properties Getter and Setter Methods: Here, we are implementing a Kotlin program to demonstrate the example of properties getter and setter methods. Variable having a class-level scope, declared inside the class body but outside the functions called property. Property can be declared with var (mutable) and val (read-only).
In Kotlin, we can define properties in the same way as we declare another variable. Kotlin properties can be declared either as mutable using the var keyword or as immutable using the val keyword. Here, the property initializer, getter and setter are optional. We can also omit the property type, if it can be inferred from the initializer.
When calling Java code from Kotlin, the compiler will automatically convert Java fields into properties if the getters and setters are properly named. If a Java class has a field accessed by a no-argument method that starts with “get” and modified with a single-argument method that starts with “set”, it becomes a var property in Kotlin.
Using a public getter method and a private or protected setter method achieves this. Kotlin provides a succinct way to implement this access pattern by allowing visibility modifiers on a property’s set () method: Now any consumers of the book class can read the inventory property, but only the Book class can modify it.
A simple approach would be to have a private var, but then to provide a public property that delegates to it:
data class Test (private var attribute_ : String) {
val attribute: String get() = attribute_
}
To add some background to the other answer:
There's no way to do this directly in the constructor, though there have been several proposals as to how it could be added to the language; see here.
If it weren't a data class, I'd suggest this alternative:
class Test(_attribute: String) {
var attribute = _attribute
private set
}
That only stores one value in the object, so is marginally more efficient.
But since this is a data class, that's not possible. (Data classes can't have non-properties in their primary constructors.) So the other answer's suggestion seems best.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With