Kotlin has one main constructs to prevent shared mutable state:
val vs var keyword
This code fails, because it reassigns to a val, you would have to change above code to a var.
This also works with java, using the "final" keyword
But there are some issues with the final keyword, mainly that it is ambigous: it is both used to declare variables immutable and to put constraints on inheritance. Furthermore you always have to ask yourself if you really want to use a var in Kotlin, while in Java you have to not forget the final keyword
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.