What is the equivalent of Java static methods in Kotlin?

AndroidMobile DevelopmentApps/ApplicationsKotlin

In Java, "static" keyword is used for efficient memory management. Once a variable or method is declared as static, then the JVM will allocate memory for these variable only once. Usually static variables are used to declare common properties of a class, for example, "Name of the institution". In the following example, we will see how to use the static keyword.

Example of Static in using Java

In order to demonstrate how static works in Java, we will access our online Java compiler and we will create a Test class. Inside Test, we will try to create a static variable along with a static method and we will access both of them without creating any of its class objects.

Example

public class Test
{
   static int myStaticVariable = 10;
   // static method
   static void staticMethod()
   {
      System.out.println("www.tutorialspoint.com");
   }

   public static void main(String[] args)
   {

      // accessing static method and variable without creating
      // any object of class Test
      staticMethod();
      System.out.println("Accessing Static variable-"+myStaticVariable);
   }
}

Output

Once you execute the code, it will generate the following output −

$javac Test.java
$java -Xmx128M -Xms16M Test
www.tutorialspoint.com
Accessing Static variable-10

Equivalent of Java Static Methods in Kotlin

In Kotlin, we don't have a static keyword. In this article, we will learn how we can achieve the same memory management using a different keyword available in the Kotlin library. The objective is to implement a different Kotlin library function to opt a condition where memory will be created only once and its value cannot be modified from other section of the application.

There are two ways to use static in Kotlin −

  • Using companion object

  • Using object class and @JvmStatic annotation

Let's understand each of these ways in detail.

Using Companion Object

Adding a companion in the object will help developers to achieve static functionality in Kotlin. It stores the object in the same file where the class is stored, hence it can access all the private methods and variables inside the class. It gets initialized along with the class initialization stage.

Example

We will try to implement a companion object and we will see how efficiently we can handle the memory management in Kotlin.

fun main(args: Array<String>) {
   // Accessing class variable and method
   //with out creating class object
   println("Hello!"+'\n' + "This an example of accessing class variable without creating object." + MyClass.staticField+'\n')
   println("Hello!"+'\n' + "This an example of accessing class Method without creating an object." + MyClass.getStaticFunction()+'\n');

}

class MyClass{
   companion object {
      val staticField = "This is a Static Variable."
      fun getStaticFunction(): String {
         return "This is a static Method."
      }
   }
}

Output

Once you execute the code, it will generate the following output −

Hello!
This an example of accessing class variable without creating
object. This is a Static Variable.

Hello!
This an example of accessing class Method without creating an
object. This is a static Method.

In this sample code, if you try to modify the value of any of the static variables, you will see Kotlin compiler will throw an error.

Example

fun main(args: Array) {

   // Trying to modify the static variable

   MyClass.staticField="Hello Students";
   println("Hello!"+'\n'+"This an example of accessing class variable with out creating object-"+MyClass.staticField+'\n')

}
class MyClass{
   companion object {
      val staticField = "This is an Static Variable"
      fun getStaticFunction(): String {
         return "This is a static Method"
      }
   }
}

Output

The above piece of code will generate the following error −

$kotlinc -nowarn main.kt -include-runtime -d main.jar
main.kt:5:5: error: val cannot be reassigned
MyClass.staticField="Hello Students";
^

Using object class and @JvmStatic annotation

As per the Kotlin documentation, once @JvmStatic annotation is applied upon any variable or method, it will work as static functionality of that class.

Example

In the following example, we will create an object class and in that object class, we will declare variables and methods using @JvmStatic annotation in order to implement static functionality in Kotlin environment.

fun main(args: Array) {
   // Accessing class variable and method
   //with out creating class object
   println("Hello!"+'\n' + "This an example of accessing a class variable without creating an object." +MyClass.staticField+'\n')
   println("Hello!"+'\n' + "This an example of accessing a class Method without creating an object. " +MyClass.getStaticFunction()+'\n');

}
object MyClass{
   @JvmStatic
   val staticField = "This is a Static Variable."
   @JvmStatic
   fun getStaticFunction(): String {
      return "This is a Static Method."
   }
}

Output

It will generate the following output in the result section . −

Hello!
This an example of accessing a class variable without creating an
object. This is a Static Variable.

Hello!
This an example of accessing a class Method without creating an
object. This is a Static Method.
raja
Updated on 27-Oct-2021 08:22:43

Advertisements