Define a compile-time constant
// val indicates that a variable cannot be changed once assigned, But assignment is done at runtime by xiaojin on 7/24/21 11:08pm val count = 1 // Use const to define compile-time constants by Xiaojin on 7/24/21 11:08pm companion object{ const val max = 100 }Copy the code
  • Compile-time constants must be at the top level of either the object declaration or a member of the CO ‘mpanion Object
  • Class as String or native type
  • You cannot have custom getters
  • Cannot be assigned in any function because it is a compile-time constant
2. Const vs. val

Val is a keyword, but const is a modifier.