Member-only story
Kotlin Companion Object: Introduce
Kotlin programming language is a popular language used on many platforms, especially Android. Kotlin was announced at the JVM Language Summit on July 19, 2011.
Companion Object ⚔️
It doesn’t allow us to create Static Properties & Static Function in Kotlin. We need to use Companion Object
so that we can create static properties. In Companion Object, we can define Constants
, Properties
, Functions
that want to be static in the class and use them comfortably.
Objects defined in Companion Object are similar to static members in other Programming Languages( Java
, C#
). In fact, Companion Object
is members of real objects at runtime, so we can use an Interface even when realized.
We will learn the companion object with a basic type example below: ⤵️
In our example, we simply created a Companion Object
. We can access Companion Object
in 2️⃣ ways. When we give a Name value, we can access it as…