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.
Kotlin code can be easily called from Java. For example, instances of a Kotlin class can be seamlessly created and operated in Java methods.
One extremely useful feature in IntelliJ IDEA and Android Studio is the ability to see the compiled bytecode of your Kotlin code and then the decompiled Java code. For this, press Ctrl+Shift+A (Cmd+Shift+A on Mac) to invoke the action search, then type “Show Kotlin Bytecode” (or “skb”) and press Enter.
And yes you can! This works as listOf , like many of our daily Kotlin utility functions, are simply static functions you can use from Java.
As a workaround, you should be able to make the Kotlin field visible using @JvmField
:
@JvmField var addressLocationBox: ToOne? = null
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With