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() // OutputOutputs Foo println(Foo.bar) . // OutputOutputs Barbar
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() // OutputOutputs Foo println(bar) . // OutputOutputs Barbar