Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: data class private setter public getter

Tags:

kotlin

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 ?
}
like image 252
Dachstein Avatar asked May 28 '19 23:05

Dachstein


People also ask

Should I use getters and setters in Kotlin?

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.

Should getter and setter methods be public or private?

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.

How do you set setters and getters in Kotlin?

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.

Should getters be public or private?

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.

What are properties getter and setter methods in Kotlin?

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).

How to declare a property in Kotlin?

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.

How do I call a Java class from Kotlin?

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.

How do you implement access pattern 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.


2 Answers

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_
}
like image 139
Louis Wasserman Avatar answered Oct 10 '22 21:10

Louis Wasserman


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.

like image 43
gidds Avatar answered Oct 10 '22 21:10

gidds