SoFunction
Updated on 2025-03-01

Detailed explanation of Kotlin standard functions and static methods application

Standard functions

  • Standard functions are functions defined in files. Any Kotlin code can freely call all standard functions.
  • The let function is a standard function, and it is often used to handle empty judgments.

with function

The with function receives two parameters. The first parameter can be any type of object, and the second parameter is a Lambda expression.

The with function will provide the context of the first parameter in the Lambda expression and use the last line of code of the Lambda expression as the return value of the function.

Sample code

val res = with(obj) {
    //This is the context of the obj object    "value"//Here is the return value of the with function}

The function of the with function. If there is a need to eat all the fruits and print the results, the code can be written like this

fun main() {
    doSomething()
}
fun doSomething() {
    val list = listOf<String>("Apple", "Banana", "Pear")
    val sb = StringBuilder()
    ("Start eating fruits.\n")
    for (fruits in list) {
        (fruits).append("\n")
    }
    ("Ate all fruits.")
    val res = ()
    println(res)
}

This is how you can write with the with function. You can see that in Lambda expressions, they are all in the context of StringBuilder. So we don’t need to call the append() method through the StringBuilder instance, but just use the append() method.

fun withTest() {
    val list = listOf&lt;String&gt;("Apple", "Banana", "Pear")
    val res = with(StringBuilder()) {
        append("Start eating fruits.\n")
        for (fruits in list) {
            append(fruits)
            append("\n")
        }
        append("Ate all fruits.")
        toString()//The last line is the return value of the function    }
    println(res)
}

Run function

  • The run function is basically similar to the with function. The difference is that the run function cannot be called directly and needs to be called on the basis of a certain object
  • Secondly, the run function only accepts one Lambda parameter and provides the context of the calling object in the Lambda expression.
  • The other functions are the same as the with function
  • Examples of using run function to complete the above requirements
fun runTest() {
    val list = listOf&lt;String&gt;("Apple", "Banana", "Pear")
    //Call out StringBuilder to call the run function    val res = StringBuilder().run {
        append("Start eating fruits.\n")
        for (fruits in list) {
            append(fruits)
            append("\n")
        }
        append("Ate all fruits.")
        toString()
    }
    println(res)
}

apply function

  • The apply function is basically the same as the run function, that is, the apply function cannot specify the return value, and can only return the caller itself.
  • apply function instance
fun applyTest() {
    val list = listOf<String>("Apple", "Banana", "Pear")
    val res = StringBuilder().apply {
        append("Start eating fruits.\n")
        for (fruits in list) {
            append(fruits)
            append("\n")
        }
        append("Ate all fruits.")
    }
    println(())
}

Static method

  • Static methods are also called class methods, which means that they do not need instances, and can be called directly using classes. Generally, the methods in the tool class are set to static methods for easy calling.
  • The static method in java is the method of modifying it using static keywords
  • But there is no direct static method in kotlin

Singleton class

If you want to define a tool class in kotlin, you will recommend a singleton class to implement it

object Util {
    fun doAction() {
        println("do something")
    }
}

Although the doAction() method in the singleton class is not statically placed, we can still use() to call it directly

However, the disadvantage of a singleton class is that the methods in the singleton class are all called similar to static methods, but what should we do if we only need to make one method in the class static?

companion object

The methods defined in the component object can be called similar to static methods.

class Util {
    //Non-static method properties    fun doAction1() {
    }
    //Have properties of static methods    companion object {
        fun doAction2() {
        }
    }
}

annotation

Singleton classes and companion object both imitate static methods in syntax form, but actually do not belong to real static methods.

If we add @JvmStatic annotation to the singleton class or methods in the companion object, the kotlin compiler will compile these methods into real static methods

as follows

class Util {
    //Non-static method properties    fun doAction1() {
    }
    //Have properties of static methods    companion object {
        @JvmStatic
        fun doAction2() {
        }
    }
}

@JvmStatic can only be added to singleton classes or methods in companion object

Top-level method

  • Top-level methods refer to methods that are not defined in any class. The kotlin compiler will compile all top-level methods into static methods.
  • Therefore, as long as a top-level method is defined, it must be a static method

This is the article about the detailed explanation of Kotlin standard functions and static methods. For more related contents of Kotlin standard functions and static methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!