How to access function of companion object using class object in Kotlin?

2.4k Views Asked by At

In Kotlin, we can access function or property of companion object using class name as following:

class DemoClass {
    companion object {
        fun someFunction() {}
    }
}

// Accessing function
DemoClass.someFunction()

But how can I access the same function if I have an object of DemoClass?

class DemoClass {
    companion object {
        fun someFunction() {}
    }
}

val demoObj = DemoClass()

// I cannot do following - It will not compile.
demoObj.someFunction() 

Basically, I want to access companion function using class object.

0

There are 0 best solutions below