• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Question - What are companion objects in Kotlin?

Answer -

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.

To create a companion object, you need to add the companion keyword in front of the object declaration.

The following is an example of a companion object in Kotlin:

class ToBeCalled {
 companion object Test {
 fun callMe() = println("You are calling me :)")
 }
}
fun main(args: Array) {
 ToBeCalled.callMe()
}

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners