object thread-safe?Yes. The initialization of an object singleton in Kotlin is thread-safe. It relies on the JVM's static initialization locking mechanism.
In Java, you often had to write:
public class Singleton {
private static Singleton instance;
public static synchronized Singleton getInstance() { ... }
}
Kotlin handles this automatically.
However, the properties inside the object are NOT automatically thread-safe. If you have a mutable var inside an object, you still need to synchronize access to it if multiple threads modify it.
Mark this when you can explain the answer in your own words.
Help fellow developers prepare for interviews
Sharing helps the Android community grow ๐