Why does companion object put const vals into parent class?

297 Views Asked by At

I have a class in Kotlin:

class AClass {
    companion object {
        const val CONST_VAL = "THIS IS A CONST VAL STRING"
        val JUST_VAL = "THIS IS A NON-CONST VAL STRING"

        fun aFunction() {}
    }
}

and a Main class in Java which is accessing companion members:

public class Main {
    public static void main(String[] args) {
        // aFunction can only be accessed by using Companion
        AClass.Companion.aFunction();
        // CONST_VAL can only be accessed from the parent class
        String constValString = AClass.CONST_VAL;
        // JUST_VAL can only be accessed with Companion
        String valString = AClass.Companion.getJUST_VAL();
    }
}

How come, in Java, both #aFunction() and JUST_VAL can only be accessed via the Companion while CONST_VAL can only be accessed via the parent class directly? Shouldn't CONST_VAL be accessed only via the Companion as well?

0

There are 0 best solutions below