• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Question - What is the use of Companion Objects in Kotlin?

Answer -

Companion Objects are required in Kotlin because Kotlin doesn't have static members or member functions, unlike Java or C#. If we need to write a function that can be called without having a class instance but needs access to the internals of a class, we can write it as a member of a companion object declaration inside that class.

For example:

class EventManager {  
    companion object FirebaseManager {  
    }    
}  
val firebaseManager = EventManager.FirebaseManager   
The companion object is a singleton, and it is a proper object which you can assign to a variable and pass it around. If you integrate with Java code and need a true static member, you can annotate a member inside a companion object with @JvmStatic.

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners