171

In Java, to declare a constant, you do something like:

class Hello {
    public static final int MAX_LEN = 20;
}

What is the equivalent in Kotlin?

iknow
  • 8,358
  • 12
  • 41
  • 68
pdeva
  • 43,605
  • 46
  • 133
  • 171

4 Answers4

270

According Kotlin documentation this is equivalent:

class Hello {
    companion object {
        const val MAX_LEN = 20
    }
}

Usage:

fun main(srgs: Array<String>) {
    println(Hello.MAX_LEN)
}

Also this is static final property (field with getter):

class Hello {
    companion object {
        @JvmStatic val MAX_LEN = 20
    }
}

And finally this is static final field:

class Hello {
    companion object {
        @JvmField val MAX_LEN = 20
    }
}
Ruslan
  • 14,229
  • 8
  • 49
  • 67
  • First example (with const field) can be used for attributes. Popular case: declare all web api paths in the single file and reference it from controllers with such code: "@RequestMapping(path = arrayOf(WebPathConstants.MapApiPath))" (Spring Boot attribute) – Manushin Igor May 24 '17 at 17:27
  • Hi guys! Do you know if this @JvmField is still necessary? I am using this in android and it shows a lint warning saying that "const" can be used instead. I changed it to const and the java class that is using it does not have any problems. – Leandro Ocampo Jan 19 '18 at 19:43
  • 1
    @LeandroOcampo it's still necessary in case if you have mutable static field, `const val` - compile time constant and for sure it can be used as replacement for `@JvmField val` in some cases. Through it doesn't work if value - calculated in runtime, or it not primitive type or String. Ref: http://kotlinlang.org/docs/reference/properties.html#compile-time-constants – Ruslan Jan 19 '18 at 23:08
43

if you have an implementation in Hello, use companion object inside a class

class Hello {
  companion object {
    val MAX_LEN = 1 + 1
  }

}

if Hello is a pure singleton object

object Hello {
  val MAX_LEN = 1 + 1
}

if the properties are compile-time constants, add a const keyword

object Hello {
  const val MAX_LEN = 20
}

if you want to use it in Java, add @JvmStatic annotation

object Hello {
  @JvmStatic val MAX_LEN = 20
}
Gary LO
  • 1,003
  • 6
  • 11
13

For me

object Hello {
   const val MAX_LEN = 20
}

was to much boilerplate. I simple put the static final fields above my class like this

private val MIN_LENGTH = 10 // <-- The `private` scopes this variable to this file. Any class in the file has access to it.

class MyService{
}
Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
Simon Ludwig
  • 1,754
  • 1
  • 20
  • 27
  • 3
    In the cases where you don't need the constant to be exposed outside the file (ie. java's `private`), this definition is the most concise. – javaxian Apr 25 '18 at 10:27
1
class Hello {
    companion object {
         @JvmStatic val MAX_LEN = 20
    }
}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
yousef
  • 1,345
  • 1
  • 12
  • 20
  • 1
    Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jun 19 '22 at 04:21