How to create a property without accessories in Kotlin?

I am using the Kotlin class from Java code. My Kotlin class is as follows:

class Something { var a = 0 } 

I want to have access to a from Java code, for example

 s = new Something(); sa = 5; 

however, I only have s.getA() and s.setA(5) . Is there a way to make this property directly configurable and Java ready? Obviously, in this case we cannot have a custom getter and setter.

+5
source share
1 answer

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

If you need to open the Kotlin property as a field in Java, you need to annotate it using the @JvmField annotation. The field will have the same visibility as the base property. You can annotate a property with @JvmField if it has a background field, is not private, has no open, override, or constant modifiers and is not a delegated property.

+10
source

Source: https://habr.com/ru/post/1245216/


All Articles