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.
Data class - Kotlin Tutorial
From the course: Kotlin for Android: Best Practices
Data class
- [Instructor] Very often, we'll define our own class to contain some attributes. Here's an example. I have the class Food, which has a name, whether it's healthy or not, and whether I can snack on it or not. So, inside main, I have defined carrots to have the name carrots, and it is healthy, and it's a snack. For readability though, this doesn't look too good, because what is true and what is true? In Kotlin, you can use named parameters. So here, I can add isHealthy =true, but you can see that there's an error that says "Mixing names and positioned arguments is not allowed", so I will also need to say isSnack=true. Yes, it makes your code a little bit longer, but personally, I prefer to have that instead of always going back to the class definition and, oh, okay, the first boolean is whether it's healthy or not, and the second one is whether it's a snack or not. We are in Kotlin Playground right now, so some IDEs like Android Studio, the newer version actually will give you a grayed…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.