What is the equivalent of Java static methods in Kotlin?

how to declare static variable in kotlin
kotlin static method
create static method in kotlin
how to declare static function in kotlin
static function in kotlin android
static method in kotlin android
kotlin call static method
kotlin static factory method

There is no static keyword in Kotlin.

What is the best way to represent a static Java method in Kotlin?

You place the function in the "companion object".

So the java code like this:

class Foo {
  public static int a() { return 1; }
}

will become

class Foo {
  companion object {
     fun a() : Int = 1
  }
}

You can then use it from inside Kotlin code as

Foo.a();

But from within Java code, you would need to call it as

Foo.Companion.a();

(Which also works from within Kotlin.)

If you don't like having to specify the Companion bit you can either add a @JvmStatic annotation or name your companion class.

From the docs:

Companion Objects

An object declaration inside a class can be marked with the companion keyword:

class MyClass {
   companion object Factory {
       fun create(): MyClass = MyClass()
   }
}

Members of the companion object can be called by using simply the class name as the qualifier:

val instance = MyClass.create()

...

However, on the JVM you can have members of companion objects generated as real static methods and fields, if you use the @JvmStatic annotation. See the Java interoperability section for more details.

Adding the @JvmStatic annotation looks like this

class Foo {
  companion object {
    @JvmStatic
    fun a() : Int = 1;
  }
}

and then it will exist as a real Java static function, accessible from both Java and Kotlin as Foo.a().

If it is just disliked for the Companion name, then you can also provide an explicit name for the companion object looks like this:

class Foo {
  companion object Blah {
    fun a() : Int = 1;
  }
}

which will let you call it from Kotlin in the same way, but from java like Foo.Blah.a() (which will also work in Kotlin).

What is the equivalent of Java static methods in Kotlin?, Static variables belong to a class and not to its instance. Same is with static methods also. Now, in Android development, things are moving from  Same is with static methods also. Now, in Android development, things are moving from Java to Kotlin and one of the biggest problems that the developers face while migrating from Java to Kotlin is making a static method because in Kotlin there is nothing as such static. Yes, you heard it right, Kotlin doesn't have a static keyword.

Docs recommends to solve most of the needs for static functions with package-level functions. They are simply declared outside a class in a source code file. The package of a file can be specified at the beginning of a file with the package keyword.

Declaration

package foo

fun bar() = {}

Usage

import foo.bar

Alternatively

import foo.*

You can now call the function with:

bar()

or if you do not use the import keyword:

foo.bar()

If you do not specify the package the function will be accessible from the root.

If you only have experience with java, this might seem a little strange. The reason is that kotlin is not a strictly object-oriented language. You could say it supports methods outside of classes.

Static Methods Behavior in Kotlin, In this quick tutorial, we'll see a few ways to achieve Java's static method behavior in Kotlin. 2. Package-Level Functions. Java Static Method equivalent in Kotlin Posted on May 29, 2017 in Basic Practice This tutorial shows you a way to represent Java static method in Kotlin using Object Expression.

A. Old Java Way :

  1. Declare a companion object to enclose a static method / variable

    class Foo{
    companion object {
        fun foo() = println("Foo")
        val bar ="bar"  
        }
    }
    
  2. Use :

    Foo.foo()        // Outputs Foo    
    println(Foo.bar) // Outputs bar
    


B. New Kotlin way

  1. Declare directly on file without class on a .kt file.

    fun foo() = println("Foo")
    val bar ="bar"
    
  2. Use the methods/variables with their names. (After importing them)

    Use :

    foo()        // Outputs Foo          
    println(bar) // Outputs bar     
    

Daily Kotlin: Static methods, If you come from Java world, you are already familiar of static methods. They prevent us from copying our methods to every class and allows  What is the equivalent of Java static methods in Kotlin? The answer? Simple, but not quite as you think. Methods can be declared static using the ‘@JvmStatic’ notation, as follows: @JvmStatic fun main() {} But then comes a very specific hurdle: that being the 'JvmStatic' notation can only be used inside an object, or a companion object.

Use object to represent val/var/method to make static. You can use object instead of singleton class also. You can use companion if you wanted to make static inside of a class

object Abc{
     fun sum(a: Int, b: Int): Int = a + b
    }

If you need to call it from Java:

int z = Abc.INSTANCE.sum(x,y);

In Kotlin, ignore INSTANCE.

Calling Kotlin from Java, Note that static method in interfaces were introduced in Java 1.8, so be sure to it manually by invoking the equivalent of the Class<T>.kotlin extension property:. There is no static keyword in Kotlin. What is the best way to represent a static Java method in Kotlin? Solution You place the function in the “companion object”. So the java code like this: class Foo { public static int a() { return 1; } } will become class Foo { companion object { …

This also worked for me

object Bell {
    @JvmStatic
    fun ring() { }
}

from Kotlin

Bell.ring()

from Java

Bell.ring()

What is the advantage of "companion object" vs static keyword , That is, a Java static method or field is a hard reference to method or field There is no exact equivalent to the java 'class method' in kotlin, the  This tutorial shows you a way to represent Java static method in Kotlin using Object Expression. ContentsI. TechnologyII. Practice1. Java static Classes and Methods2. Equivalent in Kotlin3. Run & Check Result I. Technology – Java 1.8 – Kotlin 1.1.2 II. Practice 1. Java static Classes and Methods [crayon-5e8720abe9d2b280685678/] 2.

Kotlin: Static Methods, See the many ways you can bring static methods from Java to Kotlin, including tips for Companion objects and identifiers and package-level  What is the equivalent of Java static methods in Kotlin? 0 votes. 46 views. asked Feb 5 by Hitesh Garg (355 points) Since there is no static keyword in Kotlin.

What is the equivalent of Java static methods in Kotlin?, For typical utility use cases, that would be top-level functions. For utility methods that are constrained to one class, it would be a method defined in the  One way in which the Kotlin language differs from Java is that Kotlin doesn't contain the static keyword that we're familiar with. In this quick tutorial, we'll see a few ways to achieve Java's static method behavior in Kotlin.

Java Static Method equivalent in Kotlin, This tutorial shows you a way to represent Java static method in Kotlin using Object Expression. Kotlin is a statically typed programming language for the JVM, Android and the browser, 100% interoperable with Java. JetBrains team discussing static constants in Kotlin: "Static constants" in Kotlin. Kotlin is designed so that there's no such thing as a "static member" in a class.

Comments
  • Use a package-level function.
  • Be advised: Kotlin has removed Java-style statics to encourage more maintainable (dare I say 'better') coding practices. Static globals are generally against the OOP-paradigm but they can be quite convenient. Hence, Kotlin has provided us with companions, a more OOP-friendly way of having statics.
  • Kotlin is now preferred language for Android development, according to Google.
  • In Kotlin it will be fun a(): Int { return 1 } or even fun a(): Int = 1
  • @DmitryZaitsev or even fun a() = 1.
  • What do Factory mean?
  • @BagusAjiSantoso Factory is the name of the companion object - but what can that be used for? I have no idea, but I was interested so I created a question dedicated to that: stackoverflow.com/q/45853459/221955.
  • @Yajairo87 I think what you're asking is too much to cover in a comment here - so I've created a question addressing it directly : stackoverflow.com/questions/47046474/…
  • Note that under the hood these "top-level" or "package" functions are actually compiled into their own class. In the above example, the compiler would create a class FooPackage with all of the top-level properties and functions, and route all of your references to them appropriately. More info from jetbrains.
  • +1 For explaining how to do the equivalent right in Kotlin rather than just the mirror equivalent from Java.