Below is a example of a pattern from Android (Just an example, not interested in android specifics):
/*Im a kotlin file*/
class ListItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val text: = itemView.my_view
}
Then the pattern is that you access the text field as such:
/*Im a Java file*/
holder.text.setText("Metasyntactic variable");
It's unfortunate to have a large file with a set structure doing the above and then have:
/*Im a Java file, but this particular holder is a kotlin file*/
holder.getText().setText("Metasyntactic variable");
Is it possible to solve this? Maybe with some @Jvm
annotation
Kotlin code can be easily called from Java. For example, instances of a Kotlin class can be seamlessly created and operated in Java methods.
Yes. Kotlin is 100% interoperable with the Java programming language and major emphasis has been placed on making sure that your existing codebase can interact properly with Kotlin. You can easily call Kotlin code from Java and Java code from Kotlin.
Properties and fields terminologies in Kotlin sometimes is a bit confusing because technically, Kotlin doesn't have Fields. You can't declare a field. Everything is Properties!
It's @JvmField
:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With