Pass Variable from "File2.kt" to "File1.kt"

Hey there!
First, im new in Kotlin/Java Dev.

I want to know:
Is there a Way to access Data(variable) from File B on File A?
I have multiple Class-Files in my Project and want to access the Variable-Data “username” from “File B.kt”.

Hope u understand my Problem xD


First of all, I’d like to warn you to avoid using global variables. And the same is for global state, etc.

However, you can do this by using objects, for example.

So, create object like:

object SharedData {
    var myString = "123"

Next in File1.kt you can read:

fun something() {
    SharedData.myString = "321"

And in File2.kt you can write (for example):

fun another() {
1 Like