Use as a singleton

suggest change

A Kotlin object is a singleton. You do not write SomeSingleton.INSTANCE to get it.

In Java the singleton looks like this:

public enum SharedRegistry {
    INSTANCE;
    public void register(String key, Object thing) {}
}

public class Main {
    public static void main(String[] args) {
        SharedRegistry.INSTANCE.register("a", "apple");
        SharedRegistry.INSTANCE.register("b", "boy");
        SharedRegistry.INSTANCE.register("c", "cat");
        SharedRegistry.INSTANCE.register("d", "dog");
    }
}

In Kotlin, the equivalent is:

object SharedRegistry {
    fun register(key: String, thing: Any) {}
}

fun main(args: Array<String>) {
    SharedRegistry.register("a", "apple")
    SharedRegistry.register("b", "boy")
    SharedRegistry.register("c", "cat")
    SharedRegistry.register("d", "dog")
}

It is obviously less verbose.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents