Get rid of `get` prefix for Kotlin property getters
One of the nice things Kotlin provides is interoperability with Java. Existing Java code can just call Kotlin classes/methods as if they were Java classes/methods. However, when it comes to property getters, the equivalent Java method would have a get
prefix. This is can be a little bit annoying.
Let's say we have this Experiment
class has two getters started
and ended
:
class Experiment {
val started get() = // fancy logic
val ended get() = // another fancy logic
}
When calling those two getters from Java, it'd be experiment.getStarted()
and experiment.getEnded()
. The extra get
prefix is a little redundant. There's a small trick we can do to avoid that is to use @JvmName
annotation. Just annotate the property getter like this:
class Experiment {
val started @JvmName("started") get() = // fancy logic
val ended @JvmName("ended") get() = // another fancy logic
}
Then, in Java, we now are be able to just call experiment.started()
.