I usually have this in Java:
package some.package;
public class Clz {
public static final String ACTION_DIVE = Clz.class.getName() + ".action.DIVE";
}
that is accessible from outside that class as Clz.ACTION_DIVE
and the value will be "some.package.Clz.action.DIVE"
.
How can I do the same in Kotlin class Clz
so that it can be accessed in the same way from outside Java classes? I tried the following but it does not compile because it is not a constant:
package some.package
object Clz {
const val ACTION_DIVE = Clz.javaClass.name + ".action.DIVE"
}
Clz::class.java.name
See Class references in the official Kotlin documentation.
You can use:
object Clz {
val ACTION_DIVE = Clz::class.java.name + ".action.DIVE"
}
Notice that since it's calling java
extension property the ACTION_DIVE
may not be const
.
If you really needed a const you can do you could, in older versions of Kotlin compiler, do:
object Clz {
const val ACTION_DIVE = "" + Clz::class + ".action.DIVE"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With