Kotlin Local and Global Variables Tutorial

In this section, we will learn what the local and global variables are and how they work in Kotlin.

What is Local Variable in Kotlin?

A local variable is a type of variable that is declared within the body of a function!

Such type of variable is limited to the function that is declared within! This means we can access the variable only in the enclosing function and not outside of that function!

For example, if you have two functions named A and B, and the function A had a local variable, then it wouldn’t be possible for the function B to access the value of the local variable in the function A in order to either get or change the value.

Example: creating a local variable in Kotlin

fun main(){
    var lVar = "This is a local variable in the body of the main function" 
    funcB()
}

fun funcB(){
    lVar = "changing the vlaue of the lVar"
}

Output:

error: unresolved reference: lVar

Note that we got error right at the compile time! This is because in the body of the funcB function we tried to access the value of the `lVar` which is a local variable of the main function.

Notes:

Within the body of a function, we can’t have two variables with the same name! That would be an error.

But in two independent functions, we can set the same variable name! This is again because the boundary of the two variables is independent from each other and so there won’t be any confusion when a program tries to access the variable within the body of each function.

What is global variable in Kotlin?

A variable that is declared outside the body of all functions is called the global variable.

We call it a global variable because it is accessible within the body of all functions available in the program.

For example, one function can get the value of a global variable and another one simply changes its value.

Example: creating global variable in Kotlin

var gVar = "This is a global variable"

fun main(){
    funcB()
    println(gVar)
}

fun funcB(){
    gVar = "Are you sure this is a global variable?"
}

Output:

Are you sure this is a global variable?

As you can see, the `gVar` is declared outside of the body of any functions and so it is considered as a global variable. Hence, both functions in this example are able to access the variable and read or change its value.

What is Variable Shadowing in Kotlin?

Variable shadowing happens when we have a global and a local variable, both with the same name.

In a situation like this, if within the body of the function that has the variable with the same name, we invoke that variable, it’s the local variable that will be invoked and not the global variable!

This is because Kotlin programs always start from the closest scope in order to find a variable and its value!

Basically, if you call a variable within the body of a function, your program will first start with the body of the function itself to see if the specified variable is declared there or not. Now if it found a variable with the same name, then it will invoke its value and won’t search the global scope then.

But if the target function didn’t have the specified variable, in that case only, your program will move on to the global scope to see if that environment has the mentioned variable or not.

Now when there are two variables one local and the other global and both are set with the same name, if you invoke the variable in the function, because your program starts with the function itself to search for the value of the variable, it will find the local variable and use it to resolve the target operation and so won’t check the global scope.

That’s how variable shadowing happens in a program.

Example: shadowing variables in Kotlin

var name = "global"

fun main(){
    var name = "local"
    println(name)
    funcB()
}

fun funcB(){
    println(name)
}

Output:

local

global

Note that in this example, when we called the name variable within the main function, because this function had such a variable as its local variable, then the value of this local variable is used and your program didn’t check the global scope for the mentioned variable anymore.

But when we called the same variable in the `funcB`, we can see that the value of the global variable `name` is sent to the output stream. This is because our program first checked the body of the `funcB` to see if there’s a variable with this name. But because there isn’t one, then the program continued its search for the variable in the global scope. Now here the variable exists and so our program took its value and resolved the statement `println(name)` in the body of the `funcB` function.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies