In this blog, we are going to talk about the “companion” object in Kotlin. In short today I will show you how you can declare static property and methods in Kotlin. In all the programming languages like JAVA, C#, etc. whenever we want to call a method or whenever we want to access the property of a class then we make the object of the class and with the help of that object, we access the members of the class. But in some cases, we need to access the property or method of a class directly using a class name instead of by making class instance. We can achieve this in JAVA and C# using “static” members or property. Kotlin also provides a similar feature by using the “companion” keyword. So, Let’s discuss what is static and companion keyword. If you want to learn developing an android application using Kotlin then watch my videos on Youtube https://www.youtube.com/watch?v=z2Mn-dEifzs&list=PLs8wUESuBpL4aynrdhqBwlZXxiNGCAMcB
“static” Keyword
In some languages like JAVA and C#, we use static keyword to declare the member of the class and use them without making an instance of a class. We can call static members or methods using the class name.
For example in JAVA
public class Constants {
static int USER_ID = 0;
}
In the above example as you can see we have declared the static property in Constants class. Now we can access this static property without creating a class instance.
For Example:
Constants.USER_ID
So you might be thinking that can we do this in Kotlin also? the answer is yes we can but the approach is different. Let’s see how we can declare static members in Kotlin?
Companion object in Kotlin (How to declare static members in Kotlin?)
In Kotlin, if you want to write a method or any property of the class that can be called without using an instance of the class then you have to use companion object inside the class. So by declaring companion object in Kotlinm you can access the members of the class by class name only.
For example:
class Constants {
companion object {
val USER_ID = "1001"
val FILE_EXTENSION = ".png"
fun getUserName(): String {
return "Lokesh"
}
}
}
For access property of a Constants class.
Constants.FILE_EXTENSION
Constants.getUserName()
As you can see in the above example we can create a companion object in Kotlin using the “companion object” keyword. And we can access those properties and methods without creating an instance of a Constants class. Declaring static member or companion member or property is very useful when you want some of your property can be accessed throughout your project. For example BaseUrl of APIs, File extensions, etc.
Summary
So, today we have seen use of companion object in Kotlin. and How we can use the method or property of a Kotlin class without creating an instance of a class.
Watch Youtube Video of this blog:

Lokesh Desai
Related posts
Bottom Sheet Android Kotlin
Subscribe
* You will receive the latest news and updates on your favorite celebrities!
Quick Cook!
How to use Data Class in Kotlin?
In this blog, you will learn about How to use Data Class in Kotlin? and What is Data Class? Tired…
Android Kotlin Tutorial: How to create a Class and Object ?
Today we are going to learn How to Create a Class and Object in Kotlin. Kotlin is an Object Oriented…