I have a Java class with a setter, but no getter for a property.
class Person {
private String name;
public void setName(String name) { this.name = name; }
}
I'd like to use .name=
to assign to it from Kotlin
fun example() {
val p = Person()
p.name = "example"
}
I get an error unresolved reference: name
trying to compile the Kotlin. The error goes away if I add a getter, but can I use the setter as a property without defining a getter in Java? I know I can still call p.setName("example")
from kotlin.
The above is the MCVE. The real world code is a Java builder with setters but not getters which I'd like to use from Kotlin, like
val widget = WidgetBuilder().apply {
width = 1 // instead I need .setWidth(1)
height = 2
weight = 3
name = "my widget"
}.build()