From the course: Android Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Define global constants with Kotlin

Define global constants with Kotlin - Android Tutorial

From the course: Android Development Tips

Define global constants with Kotlin

- [Instructor] When you move from Java to Kotlin you'll find that constants, values that never changed, are handled differently. First of all Kotlin has a const key word which Java lacks, so you can officially label a value as a constant as opposed to just being another variable. But they'll also placed in a specific location within classes, and you can also create global constants. Valuables that are available throughout an application. When I converted this application from Java to Kotlin I got something called a companion object. And a field that I had declared as static and final in Java, became a member of this companion object. This is because Kotlin doesn't have a concept of static class members. In order to address a value from a class declaration as opposed from a class instance, you put it inside a companion object like this. Now I'm going to add another value here, and I'll use the same syntax, private val, and I'll set this constant with a name of THE_ANSWER. And I'll give…

Contents