What is the equivalent of Java static methods in Kotlin?
If you are an Android developer and you love to make Android applications in Java, then you must have used the
static
keyword in your application to make some static variables or static methods and so on. Static variables belong to a class and not to its instance. Same is with static methods also.
Now, in Android development, things are moving from Java to Kotlin and one of the biggest problems that the developers face while migrating from Java to Kotlin is making a static method because in Kotlin there is nothing as such
static
. Yes, you heard it right, Kotlin doesn't have a
static
keyword.
So, welcome to MindOrks, in this blog, we will find what is the equivalent of static methods in Kotlin. Let's get started.
Using a companion object
In Kotlin, we can achieve the functionality of a static method by using a
companion
identifier.
For example, If you want to create a method that will return the address of your company then it is good to make this method static because for every object you create, the address is going to be the same. So, create a method named
getCompanyAddress
inside a
CompanyUtils
class by using the
companion
identifier.
class CompanyUtils {
companion object {
fun getCompanyAddress(): String {
return "MindOrks, G-773, GROUND FLOOR SUNCITY, SECTOR-54 GURUGRAM, HR"
}
}
}
After that, you can call the
getCompanyAddress
method with the name of the class wherever required:
val companyAddress = CompanyUtils.getCompanyAddress()
But if you want to call the same method in Java, then you have to add the companion keyword while calling the method:
String companyAddress = CompanyUtils.Companion.getCompanyAddress();
To get rid of the companion keyword while calling, you have to either give a name to your companion or you have to use
@JvmStatic
annotation.
Using the name for the companion
You can give a name to your companion object like below:
class CompanyUtils {
companion object myCompanionName {
fun getCompanyAddress(): String {
return "MindOrks, G-773, GROUND FLOOR SUNCITY, SECTOR-54 GURUGRAM, HR"
}
}
}
Here, in the above example,
myCompanionName
is the name of the companion. You can call the method by companion name from the Java code:
String companyAddress = CompanyUtils.myCompanionName.getCompanyAddress();
Note:
In Kotlin, you can still call the method just by the class name only i.e.
CompanyUtils.getCompanyAddress()
.
Using
@JvmStatic
annotation
If you don't want to give some name to your companion object, then you can use the
@JvmStatic
annotation with your method as below:
class CompanyUtils {
companion object {
@JvmStatic
fun getCompanyAddress(): String {
return "MindOrks, G-773, GROUND FLOOR SUNCITY, SECTOR-54 GURUGRAM, HR"
}
}
}
And then call the method from your Java class by the name of the Class of that method:
String companyAddress = CompanyUtils.getCompanyAddress();
Note:
In Kotlin, you can still call the method just by the class name only i.e.
CompanyUtils.getCompanyAddress()
.
Using a package-level function
We can use the functionality of static methods in Kotlin by making that method a package-level method.
All you need to do is make a
.kt
file and put the desired methods and properties into it without making a class. So, create a file with:
Filename: CompanyUtils.kt
Package: com.mindorks.example.staticmethodexample
Add the below method:
package com.mindorks.example.staticmethodexample
fun getCompanyAddress(): String {
return "MindOrks, G-773, GROUND FLOOR SUNCITY, SECTOR-54 GURUGRAM, HR"
}
During compilation, all the functions, properties inside the
CompanyUtils.kt
file which is inside the
com.mindorks.example.staticmethodexample
package will be compiled into static methods of the Java class named
com.mindorks.example.staticmethodexample.CompanyUtilsKt
i.e. after compilation, a Java class with
CompanyUtilsKt
name(note the addition of
Kt
word at the end of the class name) will be created and a public static method named
getCompanyAddress
will be created.
Now, in Kotlin, you can call the above method by importing the package and then directly calling it:
import com.mindorks.example.staticmethodexample.getCompanyAddress
class DemoClass {
fun someDemoMethod() {
val companyAddress = getCompanyAddress()
}
}
In Java, you can call the above methods by:
import com.mindorks.example.staticmethodexample.CompanyUtilsKt;
public class DemoClass {
public void someDemoMethod() {
String companyAddress = CompanyUtilsKt.getCompanyAddress();
}
}
If you want to change the name of the compiled class name, then you can use the
@JvmName
annotation:
@file:JvmName("NewName")
package com.mindorks.example.staticmethodexample
fun getCompanyAddress(): String {
return "MindOrks, G-773, GROUND FLOOR SUNCITY, SECTOR-54 GURUGRAM, HR"
}
Using object
You can use the functionality of static methods by putting the method inside the object block as below:
package com.mindorks.example.staticmethodexample
object CompanyUtils {
fun getCompanyAddress(): String {
return "MindOrks, G-773, GROUND FLOOR SUNCITY, SECTOR-54 GURUGRAM, HR"
}
}
And then, call the above method from your Kotlin class by;
val companyAddress = CompanyUtils.getCompanyAddress()
What Next?
In this tutorial, we saw how to use the functionality of Java static methods in Kotlin also. Now, you can use this functionality in Kotlin and make some cool Android Projects as we have on our OpenSource page .
Have a look at our Interview Kit for company preparation.
Do share this tutorial with your fellow developers to spread the knowledge. You can read more blogs on Android on our blogging website .
Apply Now: MindOrks Android Online Course and Learn Advanced Android
Happy Learning :)
Team MindOrks!
Also, Let’s connect on Twitter , Linkedin , Github , and Facebook