Kotlin static methods and variables

Kotlin

Kotlin Problem Overview


I want to be able to save a class instance to a public static variable but I can't figure out how to do this in Kotlin.

class Foo {

    public static Foo instance;
    public Foo() {
        instance = this;
    }

}

Kotlin Solutions


Solution 1 - Kotlin

The closest thing to Java's static fields is a companion object. You can find the documentation reference for them here: https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects

Your code in Kotlin would look something like this:

class Foo {

    companion object {
        lateinit var instance: Foo
    }

    init {
        instance = this
    }

}

If you want your fields/methods to be exposed as static to Java callers, you can apply the @JvmStatic annotation:

class Foo {

    companion object {
        @JvmStatic lateinit var instance: Foo
    }

    init {
        instance = this
    }

}

Solution 2 - Kotlin

It looks that you want to define a singleton object. It is supported in Kotlin as a first-class concept:

object Foo {
  ... 
}

All the boilerplate code with static field and constructor is taken care by the Kotlin automatically. You don't have to write any of that.

From the Kotlin code you can refer to the instance of this object simply as Foo. From the Java code you can referer to the instance of this object as Foo.INSTANCE, because the Kotlin compiler automatically creates the corresponding static field named INSTANCE.

Solution 3 - Kotlin

first you create a simple class then after create a block followed by companion object keyword

for example:

class Test{

    companion object{

        fun  getValue(): String{
         
           return "Test String"

        }
    }
}

you can call this class function using class name dot function name

for example:

// here you will get the function value
Test.getValue() 

Solution 4 - Kotlin

You can create a companion object for the class, and if you want the field to be static you can use the annotation @JvmStatic. Companion object have access to private members of the class it is companion for.

See below an example:

class User {
    private lateinit var name: String

    override fun toString() = name

    companion object {
        @JvmStatic
        val instance by lazy {
            User().apply { name = "jtonic" }
        }
    }
}

class CompanionTest {

    @Test
    fun `test companion object`() {
        User.instance.toString() shouldBe "jtonic"
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCaleb BasshamView Question on Stackoverflow
Solution 1 - KotlinEduard B.View Answer on Stackoverflow
Solution 2 - KotlinRoman ElizarovView Answer on Stackoverflow
Solution 3 - Kotlinrakesh rajputView Answer on Stackoverflow
Solution 4 - KotlinjtonicView Answer on Stackoverflow