Updated April 6, 2023
Introduction to Kotlin Static Function
The kotlin static function is one of the built-in function, and it is designed with some static member in the class; it recommends that we should use the packages and their level on the methods it also called, and it is used in the return type of the functions it can’t be changed whenever its initialised it also calls with other methods if it is static one else it shows the exception the static is defined with a number of ways like that directly we used, inside the companion object and create the instance and called it as inside of the object.
Syntax
We used n number of methods, variables and keywords used in the kotlin library. Moreover, the java concept is implemented on the kotlin library. Like that static is one of the keyword, variable and function for implementing the kotlin application.
package name;
class name{
companion object{
@JvmStatic
fun name(){
---some kotlin codes depends upon the requirement—
}
}
fun demo() // example method name
{
---some codes—
}
StaticFunctions.demo() // calling static function with method name
StaticFunctions.Companion.demo() // calling static function using companion along with method name
The above codes are the different ways for to use the static function on the kotlin codes.
How Static Function Works in Kotlin?
We can achieve the static functionality in kotlin by using the companion identifier. The static keyword, variable, and function will also return the function value, which depends on the type declared on the specific method. We can also use the functionality of static methods while putting the method inside the object block, and then we can able to call the method from the class. We can convert the kotlin class in which it’s declared and used by the static method to the package-level method.
All the kotlin codes are created and save it in the .kt format so that the specific methods and their properties are into it without making and utilising the classes. The @JvmStatic is the annotation used for to generate the function version model that is more ever used in the suitable java callers and classes with default methods. These annotations are used for to adding the behaviour of the function in the methods and classes it is going to be implemented in the application.
The static function mainly implemented in three ways like we declare the class and method; after that, we can call the specific method by using the StaticFunctions.method name(). Another way we can use a companion identifier or name and using the @JvmStatic method. Also, we used StaticFunctions.Companion.methodname() for implementing the logic of the static method in the application.
Examples of Kotlin Static Function
Given below are the examples of Kotlin Static Function:
Example #1
Code:
class first {
val vars = "Welcome to My Domain It’s a Nice Day Users This is the First Example regarding the static function used in the Kotlin language"
inner class second {
fun demo() = vars
}
}
class sub {
companion object {
@JvmStatic
fun vars() : Int = 1234
}
}
fun main(args: Array<String>) {
sub.vars()
val first = first()
println("This is the First Example and first input regarding the static function: ${first.second().demo()}")
println("Have a Nice Day Users Please try again")
val eg = first().second()
println("This variable which denoted and declares the inner side of the inputs on the static function ${eg.demo()}")
println("Thank you for spending and creating the Kotlin application regarding the static functionality")
}
Output:
In the above example, we used some basics for while implementing the static function for the inheritance concept that can be related to the companion object.
Example #2
Code:
object StaticFunctions {
@JvmStatic
fun demo() {
println("Welcome To My Domain it’s a Second Example that is related to the static function")
}
class second(vararg example: String) {
private val vars = example.toMutableList()
val size get() = vars.size
fun addElement(vars3: String) {
vars += vars3
println("Thank you users your additional input value is added on the separate array index which is of the string type")
}
fun get(vars1: Int): String {
println("Thank you users your additional input value is retrieved using get method the array index which is of the string type is same")
return vars.get(vars1)
}
}
}
fun main() {
StaticFunctions.demo()
val vars2 = StaticFunctions.second("ID", "Name", "Address", "City", "PinCode", "Country")
println("Your total input size is calculated: ${vars2.size}")
println("Your first input of the array which is on the Index[0]: ${vars2.get(0)}")
println("Your second input of the array which is on the Index[1]: ${vars2.get(1)}")
println("Your third input of the array which is on the Index[2]: ${vars2.get(2)}")
vars2.addElement("Area")
println("Your four input of the array which is on the Index[3]: ${vars2.get(3)}")
}
Output:
In the second example, we used the array index concept for to write the application with the static function concept. Here we used object StaticFunctions and @JvmStatic annotation for adding the elements in the array list.
Example #3
Code:
package one;
class Third {
companion object {
const val Example = "Welcomeusers"
}
fun demo() = Example;
var vars2 = 0;
fun demo4(i: Int): Int {
return 3 * i
}
fun demo5(i: Int): Int {
return 3 * i + 1
}
fun demo2(vars1: Array<Int>, i: Int, j: Int) {
var temp = vars1[i]
vars1[i] = vars1[j]
vars1[j] = temp
}
fun demo3(vars1: Array<Int>, i: Int) {
var eg = demo4(i);
var eg1 = demo5(i);
var eg2: Int;
if ((eg <= vars2 - 1) && (vars1[eg] > vars1[i])) {
eg2 = eg;
} else
eg2 = i
if ((eg1 <= vars2 - 1) && (vars1[eg1] > vars1[eg])) {
eg2 = eg1
}
if (eg2 != i) {
demo2(vars1, i, eg2);
demo3(vars1, eg2);
}
}
fun demo1(vars1: Array<Int>) {
vars2 = vars1.size
for (i in vars2 / 2 downTo 0) {
demo3(vars1, i)
}
}
fun demo(vars1: Array<Int>) {
demo1(vars1)
for (i in vars1.size - 1 downTo 1) {
demo2(vars1, i, 0)
vars2 = vars2 - 1
demo3(vars1, 0)
}
}
}
fun main(arg: Array<String>) {
println("Please enter your valuable inputs for to perfrom the heap sorts:")
var vars = readLine()!!.toInt()
println("Enter your input datas so that heap sort operation will be performed ")
var vars1 = Array(vars, { 0 })
for (i in 0 until vars)
vars1[i] = readLine()!!.toInt()
var c = Third();
c.demo(vars1)
println("Welcome To My Domain its vars1 third example for regarding the kotlin static concept")
for (i in 0 until vars)
print("${vars1[i]} ")
}
Output:
In the final example, we are writing the code for heap sort by using the static function. Here we used a companion object for to implement the static function for creating the heap sort logic.
Conclusion
In the kotlin language, static is one of the main and default keyword variables and functions for implementing the kotlin application. Here the static value cannot be changed, and moreover, the concept will be the same as the java language. The companion object is the way for to implement the static functionality on the kotlin language.
Recommended Articles
This is a guide to Kotlin Static Function. Here we discuss the introduction, syntax, and working of static functions in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –