Compile-Time Constants
Earlier we told you that var
s can have their values changed and val
s cannot.
That … was a white lie.
In fact, there are special cases where a val
can return different values, which we will discuss in Chapter 13.
If you have a piece of data that you want to be absolutely, positively immutable – to never change – consider a compile-time constant.
A compile-time constant must be defined outside any function, including main, because its value must be assigned at compile time (that is, when the program compiles) – hence the name. main and your other functions are called during runtime (when the program is executed), and the variables within them are assigned their values then. A compile-time constant exists before ...
Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.