Once a variable is defined, you can't define a new variable with the same name in the same scope. For example, the following script would not compile:
val a = 1
val a = 2 // Does not compile
print(a)
You can, on the other hand, define a variable in an inner scope that has the same name as a variable in an outer scope.
The following script would compile and run:
val a = 1;
{
val a = 2 // Compiles just fine
print(a)
}
print(a)
However, it is usually better to choose a new, meaningful variable name rather than to shadow an outer variable.
Source:
Variable scope
Login in to like
Login in to comment