Accessing Kotlin class object from Java

2019-06-16 16:54发布

问题:

I have a Kotlin class which has a class object, e.g.

public class Foo {
    public class object {
        public val SomeValue : Int = 0
    }
}

If I'm using this class from Java, how do I access SomeValue inside the class object? If it were a Java class with a static property, I'd just use Foo.SomeValue - but I can't do that here.

IntellIJ shows that I can access Foo.object.$instance, but $instance doesn't have getSomeValue or anything like that. If I try to use $instance.SomeValue anyway, when I build the error message says:

SomeValue has private access in Foo.object

I'm using Kotlin 0.5.1.

回答1:

The "absense" of getSomeValue() is a bug in the IDE. If you use it, it compiles OK. I created an issue: http://youtrack.jetbrains.com/issue/KT-3337



标签: java kotlin