How is it recommended to create constants in Kotlin? And what's the naming convention? I've not found that in the documentation.
companion object {
//1
val MY_CONST = "something"
//2
const val MY_CONST = "something"
//3
val myConst = "something"
}
Or ...?
In every programming language, we need some variable whose value will never change thoroughout the program. In Kotlin too, we have a keyword to create such a variable whose value will remain as constant throughout the program. In order to declare a value as constant, we can use the "const" keyword at the beginning.
Constants are basically variables whose value can't change. In C/C++, the keyword const is used to declare these constant variables. In Java, you use the keyword final .
To define a String constant in Kotlin, use constant and val keyword for the String. The following is a simple code snippet to define a String as constant which can be accessed using the name GREETING . const val GREETING = "Hello World!"
You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value. You declare a constant within a procedure or in the declarations section of a module, class, or structure.
Avoid using companion objects. Behind the hood, getter and setter instance methods are created for the fields to be accessible. Calling instance methods is technically more expensive than calling static methods.
public class DbConstants {
companion object {
val TABLE_USER_ATTRIBUTE_EMPID = "_id"
val TABLE_USER_ATTRIBUTE_DATA = "data"
}
Instead define the constants in object
.
Recommended practice :
object DbConstants {
const val TABLE_USER_ATTRIBUTE_EMPID = "_id"
const val TABLE_USER_ATTRIBUTE_DATA = "data"
}
and access them globally like this:
DbConstants.TABLE_USER_ATTRIBUTE_EMPID
In Kotlin, if you want to create the local constants which are supposed to be used with in the class then you can create it like below
val MY_CONSTANT = "Constants"
And if you want to create a public constant in kotlin like public static final in java, you can create it as follow.
companion object{
const val MY_CONSTANT = "Constants"
}
First of all, the naming convention in Kotlin for constants is the same than in java (e.g : MY_CONST_IN_UPPERCASE).
##How should I create it ?
###1. As a top level value (recommended) You just have to put your const outside your class declaration.
Two possibilities : Declare your const in your class file (your const have a clear relation with your class)
private const val CONST_USED_BY_MY_CLASS = 1
class MyClass {
// I can use my const in my class body
}
Create a dedicated constants.kt file where to store those global const (Here you want to use your const widely across your project) :
package com.project.constants
const val URL_PATH = "https:/"
Then you just have to import it where you need it :
import com.project.constants
MyClass {
private fun foo() {
val url = URL_PATH
System.out.print(url) // https://
}
}
###2. Declare it in a companion object (or an object declaration)
This is much less cleaner because under the hood, when bytecode is generated, a useless object is created :
MyClass {
companion object {
private const val URL_PATH = "https://"
const val PUBLIC_URL_PATH = "https://public" // Accessible in other project files via MyClass.PUBLIC_URL_PATH
}
}
Even worse if you declare it as a val instead of a const (compiler will generate a useless object + a useless function) :
MyClass {
companion object {
val URL_PATH = "https://"
}
}
###Note : In kotlin, const can just hold primitive types. If you want to assign it the result of calling a function, you need to add the @JvmField annotation. At compile time, it will be transform as a public static final variable. But it's slower than with a primitive type. Try to avoid it.
@JvmField val foo = Foo()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With