Simply you need to create a companion object and put the function in it
class UtilClass {
companion object {
// @JvmStatic
fun repeatIt5Times(str: String): String = str.repeat(5)
}
}
To invoke the method from a kotlin class:
class KotlinClass{
fun main(args : Array<String>) {
UtilClass.repeatIt5Times("Hello")
}
}
or Using import
import Packagename.UtilClass.Companion.repeatIt5Times
class KotlinClass{
fun main(args : Array<String>) {
repeatIt5Times("Hello")
}
}
To invoke the method from a java class:
class JavaClass{
public static void main(String [] args){
UtilClass.Companion.repeatIt5Times("Hello");
}
}
or by adding @JvmStatic annotation to the method
class JavaClass{
public static void main(String [] args){
UtilClass.repeatIt5Times("Hello")
}
}
or both by adding @JvmStatic annotation to the method and making static import in java
import static Packagename.UtilClass.repeatIt5Times
class JavaClass{
public static void main(String [] args){
repeatIt5Times("Hello")
}
}