What is the equivalent of Java static methods in Kotlin?

前端 未结 28 1111
眼角桃花
眼角桃花 2020-11-27 09:03

There is no static keyword in Kotlin.

What is the best way to represent a static Java method in Kotlin?

28条回答
  •  醉梦人生
    2020-11-27 09:47

    You need to pass companion object for static method because kotlin don’t have static keyword - Members of the companion object can be called by using simply the class name as the qualifier:

    package xxx
        class ClassName {
                  companion object {
                           fun helloWord(str: String): String {
                                return stringValue
                          }
                  }
        }
    

提交回复
热议问题