Private getter and public setter for a Kotlin property

Randy Sugianto 'Yuku' picture Randy Sugianto 'Yuku' · Jul 7, 2016 · Viewed 17.3k times · Source

How to make a property in Kotlin that has a private getter (or just do not have it) but has a public setter?

var status
private get

doesn't work with an error: Getter visibility must be the same as property visibility

In my case, the reason is for Java interop: I want my Java code to be able to call setStatus but not getStatus.

Answer

Alexander Udalov picture Alexander Udalov · Jul 8, 2016

It's impossible at the moment in Kotlin to have a property with a setter that is more visible than the property. There's a language design issue in the issue tracker on this, feel free to watch/vote for it or share your use cases: https://youtrack.jetbrains.com/issue/KT-3110