What is the equivalent of Java static methods in Kotlin?

前端 未结 28 1127
眼角桃花
眼角桃花 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:53

    To make it short you could use "companion object" to get into Kotlin static world like :

      companion object {
        const val TAG = "tHomeFragment"
        fun newInstance() = HomeFragment()
    }
    

    and to make a constant field use "const val" as in the code. but try to avoid the static classes as it is making difficulties in unit testing using Mockito!.

提交回复
热议问题