How to create a static method using Kotlin?

Asked

Viewed 1,764 times

5

In JAVA when we want to create a static method, we use static. Look at:

public static String getDragonGlass(){
    return String.valueOf("All the dragon glasses.");
}

And in the Kotlin, how best to represent a static method?

  • 1

    Related: https://stackoverflow.com/questions/40352684/what-is-the-equivalent-of-java-static-methods-in-kotlin

3 answers

8


Kotlin has a slightly different mechanism. Within a class:

companion object {
    fun getDragonGlass() : String = String.valueOf("All the dragon glasses.")
}

I’d call it like in Java:

Classe.getDragonGlass()

There are variants of this syntax.

Some consider this too verbose syntax, others argue that this passes the concept better and helps prevent the abuse of this type of method.

Documentation.

Kotlin has functions that are not methods, so it doesn’t even need to be within a class. But within a class to differentiate a method from a function it is necessary to include in a complementary object.

If you want to do it out of class:

fun getDragonGlass() : String = String.valueOf("All the dragon glasses.")

It has to be inside a package. So the call will be in the package context. Something like this:

Pacote.getDragonGlass()

I put in the Github for future reference.

  • 2

    The fun would be the static? Or the companion would be the static? Or has nothing to do?

  • 1

    No, the fun is keyword used in all functions, even normal methods use. This is necessary because the return type comes after the function, which is the correct location. I will complement the answer.

4

There are no static methods in Kotlin.

It is possible to create functions at the package level/package because, in Kotlin, it is not necessary to have a class to have a defined function.

Something like

package acklay.pkg

fun getDragonGlass() = "All the dragon glasses";

If it is really necessary to write a function at the class level that does not require an instance, but for example requires access to any class internal members (a Factory is a good example). It is possible to create the function as part of a "declaration of object" within the class itself.

For example:

class Classe {
    private fun foo() = object {
        val dragonGlass: String = "All the dragon glasses"
    }

    fun bar() {
        val x = foo().dragonGlass
    }
}

A form more like static, are the Companion Objects

class JonSnow {
    companion object {
        fun getDragonGlass(): String = "All the dragon glasses."
    }
}

The use would be like this

JonSnow.getDragonGlass()

See working on Try.Kotlin.

0

according to the java for kotlin, the corresponding code would be:

fun getDragonGlass():String {
    return ("All the dragon glasses.").toString()
}

The return type declaration is made after the function declaration:

fun getDragonGlass():String ...

To see conversion access the converter at the right top tire.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.