object Constant {
URL = ""
}
OR
class Constant {
companion object {
URL = ""
}
}
p
pankajrai
12/15/2019, 5:59 AM
The best approach will be
object Constant {
const val URL = ""
}
const val will inline at runtime to the place you have called it.
In those cases where inside class you want to make something static using companion object than don't forget to add @JvmStatic otherwise companion creates an object and call your method internally in the absence of @JvmStatic annotations
s
sindrenm
12/15/2019, 4:44 PM
I believe the OP likely meant this as the second alternative:
Copy code
class Constant {
companion object {
const val URL = ""
}
}
And in both cases, usages of
URL
will be inlined, as was mentioned. I agree, however, that using just a single object (
object Constant
) here is better, there's no need for any wrapping class unless it should be possible to keep different individual instances of it around. 👍