also, how do you access a top-level variable outsi...
# getting-started
f
also, how do you access a top-level variable outside a class when there is another one with the same name inside the function
l
If the variable package is
com.example.myvariables
, you can either
import com.example.myvariables.someVariable as anotherName
and use
anotherName
instead, or you can
import com.example.myvariables
and call
myvariables.someVariable
(that one won’t work if you are in the same package), or you can call directly
com.example.myvariables.someVariable
a
tl;dr: use fully qualified name for the variable
f
that is necessary even if they are in the same file?
a
You can use import aliases to simplify the access, but afaik there is no other way, e.g.:
import <very long name> as <short name>
f
ok, but there is nothing like
this
in classes?
wait, so how do I access top-level variables in the same file if they are shadowed by a local variable?
import doesn't work for that right?
a
Copy code
package test.a

val a = 1
class Test(val a: String) { fun test() { println(test.a.a) } }

fun main() {
    val test = Test("a")
    test.test()
}

>> 1
Something like this.