Static members / companion objects

Static members are shared between all instances of class.

In Kotlin static members implemented through companion object. Every class can have only one companion object. You can access to companion object members directly within the class and through operator . after the class name from outside.

If you're integrating with Java code and need a true static member, you can annotate a member within a companion object with @JvmStatic.

By default, a new anonymous class will be created for the companion object. But you can specify the name, its parent class, or interfaces, as for ordinary object.

class Demo{

	fun demoMethod(){
    // do something with sData 

    companion object{
        private var sData = mutableListOf<String>()
        val CONST_FIELD = 1
        fun someMethod1(){}
        val field2 = 2
        fun someMethod2(){} 

// using
// from Kotlin source @SinceKotlin("1.3") public abstract class Random { // ... // companion object as default instance of Random class companion object Default : Random() { private val defaultRandom: Random = defaultPlatformRandom() override fun nextInt(until: Int): Int = defaultRandom.nextInt(until) // ... } // using val rnd = Random.nextInt(100)