Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utils class in Kotlin

Tags:

kotlin

In Java, we can create an utilities class like this:

final class Utils {     public static boolean foo() {         return false;     } } 

But how to do this in Kotlin?


I try using functions inside object:

object Utils {     fun foo(): Boolean {         return false     } } 

But when call this method from Java code it need to add INSTANCE. Ex: Utils.INSTANCE.foo().


Then I change to declare it as top-level function (without class or object):

@file:JvmName("Utils") @file:JvmMultifileClass  fun foo(): Boolean {     return true } 

Then I can call Utils.foo() from Java code. But from Kotlin code I got Unresolved reference compiler error. It only allow be to use foo() function directly (without Utils prefix).


So what is the best approach for declaring utils class in Kotlin?

like image 356
nhoxbypass Avatar asked Aug 12 '18 17:08

nhoxbypass


People also ask

How do I write Utils class in Kotlin?

TL;DR: You can create utils class by putting methods inside an object , or use package-level functions. If you're integrating with Java code and need a true static utils method, you can put this method inside an object and annotate it with @JvmStatic .

What is object class in Kotlin?

In Kotlin, object is a special class that only has one instance. If you create a class with the object keyword instead of class , the Kotlin compiler makes the constructor private, creates a static reference for the object, and initializes the reference in a static block.

What is a Kotlin companion object?

Answer: Companion object is not a Singleton object or Pattern in Kotlin – It's primarily used to define class level variables and methods called static variables. This is common across all instances of the class.


2 Answers

The last solution you've proposed is actually quite idiomatic in Kotlin - there's no need to scope your function inside anything, top level functions are just fine to use for utilities, in fact, that's what most of the standard library consists of.

You've used the @JvmName annotation the right way too, that's exactly how you're supposed to make these top level functions easily callable for Java users.

Note that you only need @JvmMultifileClass if you are putting your top level functions in different files but still want them to end up grouped in the same class file (again, only for Java users). If you only have one file, or you're giving different names per file, you don't need this annotation.


If for some reason you want the same Utils.foo() syntax in both Java and Kotlin, the solution with an object and then @JvmStatic per method is the way to do that, as already shown by @marianosimone in this answer.

like image 102
zsmb13 Avatar answered Oct 11 '22 07:10

zsmb13


You'd need to use @JvmStatic for that:

In Kotlin:

object Utils {     @JvmStatic     fun foo(): Boolean = true }  val test = Utils.foo() 

In Java:

final boolean test = Utils.foo() 
like image 41
marianosimone Avatar answered Oct 11 '22 09:10

marianosimone