Declaring Variables in Kotlin With val, var and const

We have three different keywords to use to declare variables in Kotlin:

  • Var– To declare mutable variables that can change value.
  • Val– To declare read-only variables.
  • Const– To declare constants.

Val- Read-Only Variables in Kotlin

Read-only or assign-once variables in Kotlin are variables which values can never change. If possible, you should always strive to use val to declare variables to avoid possible side-effects and bugs.

Kotlin allows you to omit the type(e.g. String, Boolean) from most of the variables. This is possible because it is able to determine the type from the context of the code. Therefore, to declare a variable, we can skip the type and begin with a keyword instead. So here follows an example on how to declare and initialize a local read-only variable in Kotlin:

val name = "Ada"

If you need to, you can still add the type after the variable name:

val name: String = "Ada"

To point out, even though when you are using a read-only variable, it could still refer to a mutable object. One example of that is when we refer to a mutable collection:

val names = arrayListOf("Ada", "Grace")
names.add("James")

Now, when we would access the names list, we would have 3 names in the list instead of the initial 2.

Var- Mutable Variables in Kotlin

In case we need to use variables that can change their value, we can declare those by using the var keyword.

var count = 0
count += 2

To point out, even though the value of the variable can change, the type can never change. It will always stay fixed.

count = "two" // This does not compile as we are expecting it to be Int.

Const- Declaring Constants in Kotlin

If we have a value that never changes, is already known at compile time and is of primitive type or a String then we can declare it as a constant. To do that, we can use the const keyword. Const is an equivalent to a static final in Java.

const val NUMBER_OF_CATS = 2 

You might also wonder why even use constants in your code? Firstly, it will allow the compiler to do performance optimizations. Secondly, it will help to make the code more readable, easier to understand and possibly avoid comments in the code. In addition to that, it will make your code less error prone when we can guarantee that the value of a variable will never change.

We have a few options on where to declare constants in Kotlin. One would be to do it inside of an object.

class Animal {
    object Cat {
        private constant val NUMBER_OF_LEGS = 4
    }
}

Another option is to define it inside of a companion object instead.

class Cat { 
    private companion object { 
        private const val NUMBER_OF_LEGS = 4
    } 
}

Third option is to declare it outside of a class but inside of the same file.

class Cat { 
    companion object { 
        @JvmStatic
        fun main(args: Array<String>) {
            println(NUMBER_OF_LEGS)
        }
    } 
}

private const val NUMBER_OF_LEGS = 4

Conclusion

We have three different keywords to declare variables in Kotlin with: val, var and const. Depending on your use case, try to use the read-only references (val, const) whenever possible. And only use the mutable var if you really need the value of a variable to change.

Be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *