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

后端 未结 13 1188
不知归路
不知归路 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:21

    Values known at compile time can (and in my opinion should) be marked as constant.

    Naming conventions should follow Java ones and should be properly visible when used from Java code (it's somehow hard to achieve with companion objects, but anyway).

    The proper constant declarations are:

    const val MY_CONST = "something"
    const val MY_INT = 1
    

提交回复
热议问题