3
  • In Java class, I usually declare all my constants in a single constant file and access across the project
  • How to achieve the same in kotlin

Java Code:

public class LinksAndKeys {
    public static String BASE_URL = "http://11.111.111.11:8000/";
    public static double TAXABLE_AMOUNT = 0.18;
    public static int DAYS_INTERVAL_FOR_RATE_ME_DIALOG = 50000;
}

*What is Equivalent Kotlin code ? *

Devrath
  • 42,072
  • 54
  • 195
  • 297

1 Answers1

3

In Kotlin, we do not necessarily need to put constants in a class, so these are valid in a Kotlin source file:

const val BASE_URL = "http://11.111.111.11:8000/"
const val TAXABLE_AMOUNT = 0.18
const val DAYS_INTERVAL_FOR_RATE_ME_DIALOG = 50000

If you want to keep the LinksAndKeys namespace, you could use:

object LinksAndKeys {
  const val BASE_URL = "http://11.111.111.11:8000/"
  const val TAXABLE_AMOUNT = 0.18
  const val DAYS_INTERVAL_FOR_RATE_ME_DIALOG = 50000  
}

You can then refer to values like LinksAndKeys.BASE_URL, either from Java or Kotlin.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491