简体   繁体   中英

How to create a property without accessors in Kotlin?

I am using a Kotlin class from Java code. My Kotlin class looks like:

class Something {
    var a = 0
}

I want to be able to access a from Java code like

s = new Something();
s.a = 5;

however, I only have s.getA() and s.setA(5) . Is there any way to make this property directly settable and gettable from Java? Obviously we can't have custom getter and setter in this case.

You can annotate a property with the @JvmField annotation to expose it as a Java field.

If you need to expose a Kotlin property as a field in Java, you need to annotate it with the @JvmField annotation. The field will have the same visibility as the underlying property. You can annotate a property with @JvmField if it has a backing field, is not private, does not have open, override or const modifiers, and is not a delegated property.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM