What is the equivalent of Java static methods in Kotlin?

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

    The kotlin documents provider three ways to do that, the first is define function in package,without class:

    package com.example
    
    fun f() = 1
    

    the second is use @JvmStatic annotation:

    package com.example
    
    class A{
    @JvmStatic
    fun f() = 1
    }
    

    and the third is use companion object:

    package com.example
    
    clss A{
    companion object{
    fun f() = 1
    }
    }
    

提交回复
热议问题