What is the equivalent of Java static methods in Kotlin?

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

    For Java:

    public class Constants {
    public static final long MAX_CLICK_INTERVAL = 1000;}
    

    Equivalent Kotlin code:

    object  Constants {
    const val MAX_CLICK_INTERVAL: Long = 1000}
    

    So for the equivalent of Java static methods is object class in Kotlin.

    0 讨论(0)
  • 2020-11-27 09:43

    If you need a function or a property to be tied to a class rather than to instances of it, you can declare it inside a companion object:

    class Car(val horsepowers: Int) {
        companion object Factory {
            val cars = mutableListOf<Car>()
    
            fun makeCar(horsepowers: Int): Car {
                val car = Car(horsepowers)
                cars.add(car)
                return car
            }
        }
    }
    

    The companion object is a singleton, and its members can be accessed directly via the name of the containing class

    val car = Car.makeCar(150)
    println(Car.Factory.cars.size)
    
    0 讨论(0)
  • 2020-11-27 09:45

    This also worked for me

    object Bell {
        @JvmStatic
        fun ring() { }
    }
    

    from Kotlin

    Bell.ring()
    

    from Java

    Bell.ring()
    
    0 讨论(0)
  • 2020-11-27 09:45

    I would like to add something to above answers.

    Yes, you can define functions in source code files(outside class). But it is better if you define static functions inside class using Companion Object because you can add more static functions by leveraging the Kotlin Extensions.

    class MyClass {
        companion object { 
            //define static functions here
        } 
    }
    
    //Adding new static function
    fun MyClass.Companion.newStaticFunction() {
        // ...
    }
    

    And you can call above defined function as you will call any function inside Companion Object.

    0 讨论(0)
  • 2020-11-27 09:46

    You can use Companion Objects - kotlinlang

    Which it can be shown by first creating that Interface

    interface I<T> {
    
    }
    

    Then we have to make a function inside of that interface:

    fun SomeFunc(): T
    

    Then after, We need a class:

    class SomeClass {}
    

    inside that class we need a companion Object inside that class:

    companion object : I<SomeClass> {}
    

    inside that Companion Object we need that old SomeFunc function, But we need to over ride it:

    override fun SomeFunc(): SomeClass = SomeClass()
    

    Finally below all of that work, We need something to power that Static function, We need a variable:

    var e:I<SomeClass> = SomeClass()
    
    0 讨论(0)
  • 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
                          }
                  }
        }
    
    0 讨论(0)
提交回复
热议问题