Companion object in Kotlin

This post was originally posted at https://agrawalsuneet.github.io/blogs/companion-object-in-kotlin/ and reposted on Medium on 16th Dec 2019.
class EventManager {

companion object FirebaseManager {

}
}
val firebaseManager = EventManager.FirebaseManager
class EventManager {

companion object {

}
}
val firebaseManager = EventManager.Companion
//Java codepublic class EventManager {

private static EventManager instance;

private EventManager() {

}

public static EventManager getManagerInstance() {
if (instance == null) {
instance = new EventManager();
}

return instance;
}

public boolean sendEvent(String eventName) {
Log.d("Event Sent", eventName);
return true;
}
}
//Kotlin codeclass EventManager {

private constructor() {
}

companion object {
private lateinit var instance: EventManager

val managerInstance: EventManager
get() {
if (instance == null) {
instance = EventManager()
}

return instance
}
}

fun sendEvent(eventName: String): Boolean {
Log.d("Event Sent", eventName)
return true;
}
}

Please continue reading at https://agrawalsuneet.github.io/blogs/companion-object-in-kotlin/

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store