kotlin global constants
Kotlin Global Constants
In Kotlin, global constants can be defined using the const
keyword. Here are the steps to define and use global constants in Kotlin:
- Declare a top-level
const
property: To define a global constant, declare a top-level property using theconst
keyword. The property must be declared outside of any class or function. For example:
kotlin
const val PI = 3.14159
Here, PI
is a global constant with the value 3.14159
.
- Use the global constant: Once the global constant is defined, it can be used anywhere in the code. For example:
kotlin
val radius = 5.0
val circumference = 2 PI radius
In this example, the global constant PI
is used to calculate the circumference of a circle.
That's it! By following these steps, you can define and use global constants in Kotlin.