Static with companion objects

Kotlin doesn't have the static keyword, but to achieve compatibility with Java, it provides support for it with annotations. Kotlin provides the @JvmStatic and @JvmField annotations, which, when applied to a member of a class, make it static.

In this example, the @JvmStatic annotation will tell the compiler to compile the annotated function to a static one, and the @JvmField will tell the compiler to compile the annotated property to a static field, without getters and setters:

class Static {
companion object {

// static method
@JvmStatic
fun staticFunction() {

}

// static field
@JvmField
val staticField = 0
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.114.29