When is a string built / created / compiled?

44 Views Asked by At

When will these string get their value and should they all produce the same value?

class StringFactory{
    companion object{
        val str1 = App.shared.userSettings.getString(key, "")

        val str2: String
        get(){
            return App.shared.userSettings.getString(key, "")
        }

        val str3 = getUserKey()
        fun getUserKey():String {
            return App.shared.userSettings.getString(key, "")
        }
    }
}

When will these change their value? When the code compiles, when I read them or at some other time?

1

There are 1 best solutions below

0
On BEST ANSWER

The str1 and str3 properties are assigned on creation of StringFactory.Companion and never change their value.

In str2 the property will get the value from userSettings anytime you access it. There's no field for storing that value. It basically behaves the same as getUserKey().