Defining a static method in Java is very simple, you just need to declare a static keyword on the method:
public class Util { public static void doAction(){ ("do action"); } }
In the above code, the doAction() method is a static method. Calling a static method does not require creating an instance. Therefore, the static method is very suitable for writing some tool classes, because the tool class does not need to create an instance, and is basically globally common.
Unlike most, Kotlin has greatly weakened the concept of static methods. It is not easy to define a static method in Kotlin.
So why is Kotlin designed like this? Because Kotlin provides a syntax feature that is better used than static methods, which is a singleton class.
Functions like tool classes are implemented in Kotlin by directly recommending singleton classes. For example, the above Util tool class can be written in Kotlin as follows:
object Util { fun doAction(){ println("do action") } }
The singleton class will affect that all methods in the class will become static methods similar to static methods. If we only want to make a method in the class become static methods, we can use the following writing method:
class Util { fun doAction(){ println("do action") } companion object{ fun doAction2(){ println("do action2") } } }
Here a doAction2() method is defined in the component object code block. Now there is a fundamental difference between the two methods in the Util class. The doAction() method must first create an instance of the Util class before it can be called, while the doAction2() method can be called directly using the Util.doAction2() method.
However, the doAction2 method is not a static method. The keyword companion object actually creates a companion class inside the Util class, and the doAction2 method is an instance method defined in the companion class. It's just that Kotlin will ensure that there will always be only one companion class object in the Util class. Therefore, calling the Util.doAction2 method is actually calling the doAction2 method of the companion object in the Util class.
From this we can see that Kotlin does not directly define keywords for static methods, but provides some syntax features to support writing methods similar to static method calls.
If you really want to define a real static method, Kotlin still provides two implementation methods, annotation and top-level methods.
Top-level methods This article will not be explained, please look for other articles.
Let’s look at the annotation. The singleton class and component object used earlier only syntactically imitate the calling method of static methods. If you call it in the form of static methods in Java code, you will find that these methods do not exist. And if we add @JvmStatic annotation to the singleton class or methods in the companion object, Kotlin will compile these methods into real static methods in the compiler
class Util { fun doAction(){ println("do action") } companion object{ @JvmStatic fun doAction2(){ println("do action2") } } }
Note that the @JvmStatic annotation can only load on a singleton class or a method in a companion object. If you try to add it to a normal method, it will directly prompt a syntax error.
In this way, whether in Kotlin or Java, Util.doAction2() can be used to call it.
This is the end of this article about the use of Kotlin static methods. For more related contents of Kotlin static methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!