kotlin const val Const ‘val’ are only allowed on top level or in objects

const vals cannot be in a class. For you, this means you need to declare it top-level, in an object, or in a companion object (which also is exactly what the error message says).

Since your value is private, a companion object is one of the two options you can use:

class TradersActivityTest {
    ...
    companion object {
        private const val ONE_TR = "no_wallets.json"
    }
}

Doing that makes it accessible to the class only.


The second option is top-level. However, note that this exposes it to the rest of the file, not just the one class:

private const val ONE_TR = "no_wallets.json"

...

class TradersActivityTest {
    ...
}

For the sake of completeness, the third option was using an object:

object Constants {
    const val ONE_TR = "no_wallets.json"
}

However, it needs to be public to be accessed. It can alternatively be internal, but it again depends on what you want to have access.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)