Which method should I use is better? ```object Con...
# android
a
Which method should I use is better?
Copy code
object Constant {
    URL = ""
}

OR

class Constant {
    companion object {
        URL = ""
    }
}
p
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
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. ­čĹŹ