https://kotlinlang.org logo
#android
Title
# android
k

KotlinLeaner

07/28/2022, 8:57 AM
Hey guys, I am new in kotlin. I want to ask what is the best way of doing declare const val.
Copy code
companion object {
   const val TABLE_USER_ATTRIBUTE_EMPID = "_id"
   const val TABLE_USER_ATTRIBUTE_DATA = "data"
}
vs
Copy code
object DbConstants {
    const val TABLE_USER_ATTRIBUTE_EMPID = "_id"
    const val TABLE_USER_ATTRIBUTE_DATA = "data"
}
which is one recommended way ?
j

Jiddles

07/28/2022, 4:18 PM
Depends on scope IMO, If you have a class like
Database
and it was the best place to store the consts (e.g. cos it might be the only place it’s used), I’d use companion object in that class However if there’s no one place suited to store your constants , go with the second approach There really isn’t much different between the 2 approaches to be honest
2 Views