Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating "public" constants in a kotlin class [duplicate]

Perhaps it was bad practice but in Java I would often create something like:

public class MyService extends Service {

  public static final String ACTION_CONNECTED = "blablabla";

...
}

And reference it in another class like:

MyService.ACTION_CONNECTED

This was great. I could keep my constants nicely associated with their class.

I can't seem to find an equivalent in Kotlin. I see solutions flying around suggesting people create constants files (objects) but I don't think that's very elegant. I want there to be some way to expose a top-level const val BLAB outside its file so I can keep my ClassName.CONSTANT syntax going but it doesn't look like it's in the cards.

Is there (and what is it) a Kotlin equilivant to the good old public static final with regard to sharing constants between classes?

like image 745
kkemper Avatar asked Oct 30 '25 07:10

kkemper


1 Answers

class MyService  {
    companion object {
        @JvmStatic const val ACTION_CONNECTED = "blablabla"
    }
}

MyService.ACTION_CONNECTED 

This will be the equivalent of public static final for kotlin

like image 140
Destroyer Avatar answered Oct 31 '25 22:10

Destroyer