I have the following Kotlin class on Android:
class ThisApplication: Application() { lateinit var network: INetwork override fun onCreate() { super.onCreate() network = Network() } }
Now any external class can get the INetwork link by simply doing:
application.network
However, it also allows the outer class to overwrite this value:
application.network = myNewNetworkReference
I want to avoid the second option. Unfortunately, I cannot create a val field because it must be initialized inside the onCreate .
I also thought about making the field private and exposing it through a function, for example:
private lateinit var network: INetwork fun getNetwork() = network
However, whoever calls getNetwork () can still assign a new value to it, for example:
application.getNetwork() = myNewNetworkReference
How can I make a network field read-only by external classes? Or even better, is there a way to do this val , although I cannot initialize it inside the constructor?
source share