Best way to represent static final field without incurring a bytecode cost

I’m trying to find out what is the best way to represent static final fields in Kotlin. Imagine this simple case:

class Foo {
public static final String BLA=“bla”
// non static members below

While I can represent this as a companion object like this:
class Foo {
companion object {
const val BLA = “bla”

It generates two classes in the resulting bytecode - one for Foo and the other one for the companion object - that’s fairly expensive. Is there a way how to avoid this? This seems to be fairly expensive, especially when on Android many classes always define just one static final field (TAG) …

Any suggestion?

Have you tried adding @JvmStatic?

While that generates the static field in the outer class, it still generates the companion class … so still two classes present in the bytcode

I’d just use a top-level ‘val’ (or ‘const val’) property to represent it:

const val BLA = "bla"

class Foo {
   // instance members

The Kotlin compiler will create a top-level class anyway so this won’t incur any extra overhead in the case of a normal ‘val’.

A ‘const val’ is of course a compile time constant and will therefore be replaced at compile time by its value.

Top level val generates top level class (${filename}kt, so we are back to the same problem. Two classes instead of one.

At this time there is no other solution.