1
  • 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
  • 39,949
  • 51
  • 178
  • 266

1 Answers1

2

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
  • 954,112
  • 185
  • 2,315
  • 2,367