Constants in Kotlin — what's a recommended way to create them?

后端 未结 13 1179
不知归路
不知归路 2020-12-04 18:45

How is it recommended to create constants in Kotlin? And what\'s the naming convention? I\'ve not found that in the documentation.

companion object {
    //1         


        
13条回答
  •  臣服心动
    2020-12-04 19:08

    In Kotlin, if you want to create the local constants which are supposed to be used with in the class then you can create it like below

    val MY_CONSTANT = "Constants"
    

    And if you want to create a public constant in kotlin like public static final in java, you can create it as follow.

    companion object{
    
         const val MY_CONSTANT = "Constants"
    
    }
    

提交回复
热议问题