HixField
HixField

Reputation: 3776

Object in Kotlin loosing it var values?

I recently made the transition from iOS/Swift to Androi/Kotlin. I am using a object for encapsulating Analytics functionality (as I did in Swift):

Object Analytics{
var connection: AnalyticsConnection? = null
fun sendEvent(name: String)...
init {
connection = //allocate here
}
}

I heard (one of our Android devs mentioned it) that in the latest version of kotlin the var connection could actually loose it value at some point spontanically? I find this very weird, is this true? It seams that vars at global scope get cleared out at some point?

Upvotes: 0

Views: 279

Answers (1)

Gil G
Gil G

Reputation: 1869

It should not lose the values it contains because when you create a new class as an object you create it as a thread-safe singleton.

Which means unless you directly change the value somewhere else, the value should stay the same as the original value.

That's all in case your application is still alive, in case your application is killed/ destroyed the whole class would also be destroyed with it.

Upvotes: 3

Related Questions