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 ? *

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.