final and const keywords | Kotlin
1 min readFeb 7, 2023
final
- Is a modifier in Kotlin that can be applied to a class, function, or property, enforce encapsulation.
- A class with final cannot be subclassed.
- Applied to function or property those cannot be overridden.
- Default modifier.
class Button { }
const
- Declare a constant value, cannot be changed at runtime.
- Constant must be a primitive type (Byte, Short, Int, Long, Float, Double, Boolean, Char) or String (first-class citizen).
- Constants are defined using the “const” keyword followed by the “val” keyword.
const val = 3.141592
The val keyword in Kotlin is used to declare a variable that is assigned a value when it is created and cannot be reassigned afterwards.
Hey, mobile developers! Are you tired of boring content that puts you to sleep? My channel is anything but boring! With exciting videos on everything from coding challenges to mobile app reviews, I promise to keep you entertained and informed. So why not join my community of mobile enthusiasts and let’s have some fun? Hit that follow button and let’s get started!