Avatar ·

Constants in Kotlin: proper ways to declare and use

Hello everyone! I just started learning Kotlin after Java, and I'm confused about declaring constants. In Java, I used to write `public static final String MY_CONST = "value";`, but in Kotlin I see several different approaches and don't understand which one is correct.\n\nFor example, I see that you can write:\n\n```kotlin\nclass MyClass {\n companion object {\n val MY_CONST = "value"\n }\n}\n```\n\nOr even simply:\n\n```kotlin\nval MY_CONST = "value"\n```\n\nBut in some guides they write about `const val`, while in others they say that `const` only works with primitives. I'm also confused about `object` and `companion object` — when to use which? And should I even use `companion object` as an analog of `static final` from Java, or is it an anti-pattern?\n\nPlease tell me which approach is considered correct and