Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin constructor parameter without getter

Tags:

kotlin

I have the following code:

class ViewHolder(itemView: View, val ui: ViewUI)

I don't want that the field ui have a getter, but kotlin is generating it by default since it is a val defined in the constructor.

In the properties and fields document I found that I can generate a private getter with the following code

var setterVisibility: String = "abc"
    private set

But I can't find a way to do this in the constructor definition

like image 285
jonathanrz Avatar asked Jan 04 '23 23:01

jonathanrz


1 Answers

You can make the entire property private in the constructor, that way it will only be accessible inside the class:

class ViewHolder(itemView: View, private val ui: ViewUI)
like image 100
zsmb13 Avatar answered Jan 16 '23 01:01

zsmb13