https://kotlinlang.org logo
m

mzgreen

08/28/2016, 12:25 PM
I’ve recently discovered that we can create an enum without constants but with a method:
Copy code
// Kotlin
enum class A {
; //this is important - without it compiler won’t like it
    fun a() {}
}

//Java
enum A {
; //this is important - without it compiler won’t like it
    public void a() {}
}
but without constants there is no way to call this method. In Java we can make it
static
and then it can be called. In kotlin it has to be wrapped in a
companion object
. My question is - why such a thing even exists? Why compiler allows it to compile? I can’t see any use case for this. Any ideas?