Answers for "kotlin is an object a companion"

2

companion object in kotlin

In Kotlin, if you want to write a function or any member of the class that 
can be called without having the instance of the class then you can write 
the same as a member of a companion object inside the class.

class Comp{
  companion object {
  	val tag = "MY_TAG"
    fun sum(i:Int , j:Int) = println(i+j)
  }
}
Posted by: Guest on November-26-2021
11

companion object kotlin

class Test {
companion object {
        var mInteger: Int = 10
        fun hello() = println("hello world !")
    }
}

fun main(args: Array<String>) {
    print(Test.mInteger)
    print(Test.hello())
}
Posted by: Guest on August-24-2021

Browse Popular Code Answers by Language