Constants in Kotlin: how to properly create and use them?
Hello everyone! I've just started learning Kotlin after Java, and the topic of constants confuses me. In Java, I'm used to writing `public static final` for global constants and `final` for local ones. But in Kotlin, it's all different.
For example, I want to declare the maximum number of login attempts in an application – 5. And a constant with the application name "MyApp". Here's how I tried to do it:
```kotlin
// Option 1: inside a class
class LoginManager {
val MAX_ATTEMPTS = 5
val APP_NAME = "MyApp"
}
// Option 2: at the file level
val MAX_ATTEMPTS = 5
val APP_NAME = "MyApp"
```
But I read that using `val` inside a class for constants is bad practice because they are not real compile-time constants. And at the file level, they will still be tied to a companion object if the class is loa
No answers yet.
Log in to leave an answer