There is no static
keyword in Kotlin.
What is the best way to represent a static
Java method in Kotlin?
except Michael Anderson's answer, i have coding with other two way in my project.
you can white all variable to one class. created a kotlin file named Const
object Const {
const val FIRST_NAME_1 = "just"
const val LAST_NAME_1 = "YuMu"
}
You can use it in kotlin and java code
Log.d("stackoverflow", Const.FIRST_NAME_1)
You can use Kotlin's extension function
created a kotlin file named Ext, below code is the all code in Ext file
package pro.just.yumu
/**
* Created by lpf on 2020-03-18.
*/
const val FIRST_NAME = "just"
const val LAST_NAME = "YuMu"
You can use it in kotlin code
Log.d("stackoverflow", FIRST_NAME)
You can use it in java code
Log.d("stackoverflow", ExtKt.FIRST_NAME);
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).
The kotlin documents provider three ways to do that, the first is define function in package,without class:
package com.example
fun f() = 1
the second is use @JvmStatic annotation:
package com.example
class A{
@JvmStatic
fun f() = 1
}
and the third is use companion object:
package com.example
clss A{
companion object{
fun f() = 1
}
}
There are 2 ways you can apply static in Kotlin
First make a companion object under class
For ex:
class Test{
companion object{
fun isCheck(a:Int):Boolean{
if(a==0) true else false
}
}
}
you can call this function as
Test.Companion.isCheck(2)
Another way we can use is to make an object class
object Test{
fun isCheck(a:Int):Boolean{
if(a==0) true else false
}
}
Happy Coding!
To make it short you could use "companion object" to get into Kotlin static world like :
companion object {
const val TAG = "tHomeFragment"
fun newInstance() = HomeFragment()
}
and to make a constant field use "const val" as in the code. but try to avoid the static classes as it is making difficulties in unit testing using Mockito!.
Write them directly to files.
In Java (ugly):
package xxx;
class XxxUtils {
public static final Yyy xxx(Xxx xxx) { return xxx.xxx(); }
}
In Kotlin:
@file:JvmName("XxxUtils")
package xxx
fun xxx(xxx: Xxx): Yyy = xxx.xxx()
Those two pieces of codes are equaled after compilation (even the compiled file name, the file:JvmName
is used to control the compiled file name, which should be put just before the package name declaration).