From the course: Android Development Essential Training: App Architecture with Kotlin

Unlock the full course today

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

Create companion object functions

Create companion object functions - Android Tutorial

From the course: Android Development Essential Training: App Architecture with Kotlin

Start my 1-month free trial

Create companion object functions

- [Instructor] So far, my Android app simply presents a set of dice and a string. But I want it to be dynamic. When the user presses a button, I want to roll the dice and see a randomized set of five dice and a string that indicates what kind of result I got. And I should be able to do this over and over again and see different results. So I need some business logic. When implementing business logic that's unrelated to an app's visual presentation, such as getting a set of randomized numbers, it's a good practice to define it in a separate class of some kind, particularly when the logic doesn't really have anything to do with Android and could work on any Java based platform. You can consider creating the logic in a class' companion object. Companion object functions in Kotlin are like static methods in Java. They don't require instantiating their parent classes, and they release any memory or resources when they're complete. I'm going to create this new class in a new package that…

Contents