I am interested in what you think is the best practice where to put (private, or otherwise) “static” constants in Kotlin.
In Java, there is one way. In Android for example, one often has a constant for the log tag:
public class ThingDoer {
private static final String TAG = "Thing";
public doThing() { Log.i(TAG, "hello"); }
}
In Kotlin, there are three:
-
companion object as outputted by Java → Kotlin converter
class ThingDoer { fun doThing() { Log.i(TAG, "hello") } companion object { private const val TAG = "Thing" } }
-
file-private constant
class ThingDoer { fun doThing() { Log.i(TAG, "hello") } } private const val TAG = "Thing"
-
Don’t care about it being “static” (IntelliJ will show a weak warning by default because variables should by convention not be written starting with capital letters)
class ThingDoer { private val TAG = "Thing" fun doThing() { Log.i(TAG, "hello") } }