A. Old Java Way :
Declare a
companion objectto enclose a static method / variableclass 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
.ktfile.fun foo() = println("Foo") val bar ="bar"Use the
methods/variableswith their names. (After importing them)Use :
foo() // Outputs Foo println(bar) // Outputs bar