public class Singleton {
private static Singleton instance = null;
private Singleton(){
}
public synchronized static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
doSomeInitializationIfRequired();
return instance;
}
}
here getInstance() method is called whenever we request for the Instance where we can add code if we want to do something when the instance is called from any where everytime.
is there some way to override instance variable get() like this with Kotlin Objects
e.g.
object SomeSingleton {
get() = {
doSomeInitializationIfRequired()
}
}
I know i can write
init {
}
but that will be called only once.
doSomeInitializationIfRequired()method whenever the singleton is referenced? Maybe also tell us what you're doing in said method