Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Equivalent of Java Static Methods in Kotlin

  • Last Updated : 27 Dec, 2021

Prerequisite: static Keyword in Java

Static Keyword in Java is a non-access modifier. It is useful for representing meta-data (usually data related to a class). The static keyword serves for efficient memory management as in spite of recurring usages the Java Virtual Machine only once allocates memory for the variable, method, class or block declared as static. Static members are not related to instances being created but are owned by the class itself. In Kotlin however, we don’t have such keywords. So this functionality has to be achieved using some other ways. The most popular methods of implementing functionality similar to that of the static keyword are listed below:

By the use of companion object

Object expressions or Object declarations in Kotlin are used to create instances of an anonymous class. Using companion identifiers along with such object expressions or declarations facilitates members of the companion object to be called just by using the class name as the qualifier. When the matching class is loaded (resolved), a companion object is created that has the same semantics as a Java static initializer.

Kotlin




class GFG {
      companion object{
          fun getMetaData() : String {
            return "Static Method of Class"
        }
      }
}  
  
fun main(args: Array<String>) {
      // Calling method of companion object  
      println(GFG.getMetaData())
}

Output:

Static Method of Class

In the case of Java, the method of companion object can be called but we need an additional ‘Companion’ field to be added before the function call after the class name. We can get rid of this too, by either giving our own name to the companion object or by using @JvmStatic annotation.

By using @JvmStatic annotation

Adding @JvmStatic annotation simply before the members to be declared as the equivalent of static ones in Java works well in Kotlin to provide the same functionality.

Kotlin




object MyClass{
   @JvmStatic
   fun getMetaData(): String {
      return "Static Method of the Class"
   }
}
  
fun main(args: Array<String>) {
    println(GFG.getMetaData())
}

Output:

Static Method of Class

By using a package-level function

An equivalent functionality as that of the static members and methods in Java can also be achieved by making that member, method, class, or block be a package-level member. This can be done by creating a .kt file and placing all such desired members into it without making any class inside. In this way, all these members will be compiled by the JVM as the static parts of the Kotlin class in which the package is being imported.

Kotlin File to achieve the functionality of static keyword:

Kotlin




package com.gfg.sample
fun getMetaData() : String {
    return "Static Method of Package"
}

Main Kotlin File:

Kotlin




import com.gfg.sample
  
fun main(args: Array<String>) {
    println(getMetaData())
}

Output:

Static Method of Package

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!