global variable in class in kotlin code example

Example 1: kotlin global variable

var x = 100

fun fn() { 
    x = x + 100 
}

fun main(args: Array<String>) {
    println("X Value : $x")
    
    fn()
    
    println("X Value : $x")
}

Example 2: kotlin global variable

In Kotlin, A global variable is a variable that is declared at the top of the program and outside all functions similar to C and C++. A local variable can only be used in the particular block where it is declared. A global variable can be used in all functions.

Tags:

Misc Example