Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does it depend on, whether you can or cannot call a Java getter/setter method using Kotlin's property access syntax, in one case or another?

In some cases you are able to use property access syntax to reference Java getters/setters, in some cases you aren’t. What defines this behaviour?

val tv = TextView(this)
tv.setText("") // WEAK WARNING: Use of setter method instead of property access syntax
tv.text = "" // OK

val iv = ImageView(this)
iv.setImageResource(R.drawable.ic_launcher_background) // no weak warning, meant to be used that way
iv.imageResource = R.drawable.ic_launcher_background // ERROR: Unresolved reference: imageResource

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From the Kotlin documentation:

Note that, if the Java class only has a setter, it isn’t visible as a property in Kotlin because Kotlin doesn’t support set-only properties.

ImageView has a setImageResource but no getImageResource, so it falls into the category mentioned above.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading