There is no static
keyword in Kotlin.
What is the best way to represent a static
Java method in Kotlin?
A. Old Java Way :
Declare a companion object
to enclose a static method / variable
class Foo{
companion object {
fun foo() = println("Foo")
val bar ="bar"
}
}
Use :
Foo.foo() // Outputs Foo
println(Foo.bar) // Outputs bar
B. New Kotlin way
Declare directly on file without class on a .kt
file.
fun foo() = println("Foo")
val bar ="bar"
Use the methods/variables
with their names. (After importing them)
Use :
foo() // Outputs Foo
println(bar) // Outputs bar