Static initialisation block in Kotlin

后端 未结 2 1328
谎友^
谎友^ 2021-02-03 16:39

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

2条回答
  •  别那么骄傲
    2021-02-03 17:20

    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

提交回复
热议问题