Junior — Middle
What Java code is generated as a result of compiling a singleton object created with a Kotlin object?
sobes.tech AI
Answer from AI
Kotlin object for singleton is compiled into Java as a class with a private constructor and a static field INSTANCE that contains the single instance of this class. Additionally, a static initialization block is created where INSTANCE is initialized.
A simplified example of the structure in Java:
public final class MySingleton {
public static final MySingleton INSTANCE;
private MySingleton() {
// private constructor
}
static {
INSTANCE = new MySingleton();
}
// singleton methods
}
Thus, access to the singleton from Java is through MySingleton.INSTANCE.