3

I need to define a variable and acces it like static variables in java. I want to acces this variable just by name of a class.

2 Answers 2

3

Put it inside of a companion object in your class

class Example {
    companion object { 
          val foo = 1
     }
}

Example.foo

If everything inside of your class is going to be static then you can make that class an object in Kotlin

object Example {

        val foo = 1
 }

Example.foo
Sign up to request clarification or add additional context in comments.

Comments

0

You need to make companion object inside that class here is example using RoomDatabase Aechitecture components

class MyDatabase: RoomDatabase() {

companion object {

        var instance: MyDatabase? = null
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.