I have a Kotlin class which has a class object, eg
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.
作为一种解决方法,您应该能够使用@JvmField
使 Kotlin 字段可见:
@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
data class YourClass(@JvmField val name: String)
To set a custom name for the generated Java class, use the @JvmName annotation:
Kotlin
@file:JvmName("DemoUtils")
package com.example
class Util
fun getTime() { /*...*/ }
Java
new com.example.Util();
com.example.DemoUtils.getTime();
Moreover, if you use the @JvmMultifileClass , then:
@file:JvmName("Utils")
@file:JvmMultifileClass
package com.example
fun getDate() { /*...*/ }
Java
com.example.Utils.getDate();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.