What is the equivalent of a static initialisation block in Kotlin?
I understand that Kotlin is designed to not have static things. I am looking for something with equiva
From some point of view, companion objects in Kotlin are equivalent to static parts of Java classes. Particularly, they are initialized before class' first usage, and this lets you use their init
blocks as a replacement for Java static initializers:
class C {
companion object {
init {
//here goes static initializer code
}
}
}
companion object {
// Example for a static variable
internal var REQUEST_CODE: Int? = 500
// Example for a static method
fun callToCheck(value: String): String {
// your code
}
}
An object declaration inside a class can be marked with the companion keyword.And under this we can use like java static method and variable.LIke classname.methodname or classname.variablename