Home > front end >  Importance of var keyword in Kotlin enum class constructor declaration
Importance of var keyword in Kotlin enum class constructor declaration

Time:10-24

 enum class Admin(myName:String, val id:Int, val age:Int){
    ROOT_ADMIN ("Pete", 1, 55),
    ACADEMIC_ADMIN("Jacob",11,56),
    DEPARTMENT_ADMIN("Robin",111,50),
    CLASS_ADMIN("Chris",1111,22)

To access the properties of objects of enum class Admin, when I type

Admin.CLASS_ADMIN.____

Naturally, myName to come out in the IDE auto-complete is expected. But its not happening. But id and age does come as they have val keyword associated with them.

But when I add var in front of myName, like:

enum class Admin(var myName:String, val id:Int, val age:Int)

I am now getting myName in auto-complete.

What is the importance of var keyword here?

Note: I am aware of the fact that when we declare variables with var or val keywords in constructor, it declares a property inside that class. But how this logic relates to this situation?

CodePudding user response:

This is more about Kotlin properties and less about how val/var work with enums. In fact for most of this answer, we can completely ignore the fact that we're even talking about enums, as opposed to any other Kotlin class (but I do have a note at the end on this).

For background, when you create an instance of a class in Kotlin and provide arguments to its constructor, if those arguments have var or val, Kotlin will treat them as properties. If not, it treats them as an argument to the constructor (these can be used in init blocks, for example but do not get turned into properties).

That's what is happening in your case. Kotlin treats myName as a constructor argument and effectively throws it away as you aren't using it. It does not get turned into a property. For id and age, you've specified they are val, so Kotlin turns them into read-only properties.

As for var, when Kotlin sees this it makes them into a read/write property (they can change).

Basically: Kotlin turned id and age into read-only properties and myName was defined as a constructor argument. This is why autocomplete did not offer you myName, it wasn't a property.

Some general advice: I would absolutely not declare any mutable properties on an enum (so, use val only for read-only properties). By using var, you'll get mutable read/write properties. Normally that's fine but with enum specifically there is an expectation that they do not change, ever. You are declaring a fixed set of values (an enumeration of them!) whose internal properties do not change. As a developer if I saw an enum whose internal state was mutable, it would immediately seem wrong.

CodePudding user response:

Since item of enum class is acting like object in Kotlin (just for understanding), if you declare property as var of enum class, you could change the property value and it affects everywhere. This might be hard to understand. You can see below example code.

enum class Test(var a: String) {
    A("a"),
    B("b");
}

fun main()
{
    println(Test.A.a) // a

    Test.A.a = "b"

    println(Test.A.a) // b
}

Usually, you might not want to declare a property as mutable for the design.

  • Related