Thursday 21 December 2017

Kotlin: const: Compile time constants

You can define the compile time constants using ‘const’ keyword.

Ex
const val VENDOR_NAME = "Self Learning Java blog"

HelloWorld.kt
const val VENDOR_NAME = "Self Learning Java blog"
const val PI = 3.14

fun main(args: Array<String>) {
 println("Vendor Name $VENDOR_NAME")
 println("Value of PI : $PI")
}

Output

Vendor Name Self Learning Java blog
Value of PI : 3.14

While defining compile time constants, the properties must fulfil below preconditions.

a.   Top-level or member of an object
b.   Initialized with a value of type String or a primitive type
c.   No custom getter



Previous                                                 Next                                                 Home

No comments:

Post a Comment