From the course: Kotlin for Android: Best Practices

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

On-demand property: Getters

On-demand property: Getters - Kotlin Tutorial

From the course: Kotlin for Android: Best Practices

Start my 1-month free trial

On-demand property: Getters

- [Instructor] Look at line 21 it. It says greeting exclamation point, exclamation point. This is because we don't know whether greeting's going to be instantiated yet. So we are going to tell Kotlin if it happens to be now crash the program. One way to get rid of that is to use a getter. Go to line 10 where we define the variable greeting. Instead of calling it a text view question mark. We are going to delete that and we are going to have it so that it's associated with a getter. Get and then equal and we can copy line 17 to it from the find view by id point. And then change var to val. Let me explain what this is. Instead of var which means is a variable, we're saying its val because it quote doesn't change. It's a little bit tricky with a getter because what it means in this case is that instead of it doesn't change. It's the logic to define this variable it not changing. The get parenthesis is a function. It means every time somebody wants the value of the greeting variable go…

Contents