I have a class like this:
class MyClass {
private val myLock = ReentrantLock()
...
}
In java I would make this field final to provide memory guarantees: https://stackoverflow.com/a/27254652/2674303
Does val provides the same guarantees ?
Yes, at least on Kotlin/JVM. For other platforms, probably not.
While I couldn't find authoritative documentation on this (The Kotlin/JVM spec still hasn't been written), a private
valdeclared in the way your example does, compiles to afinalfield in bytecode. You can see this by inspecting the .class file. It is unlikely that they will change this to generate a non-finalfield in the future, because as you pointed out,finalfields have certain guarantees, and removingfinalwill be a breaking change.Of course, if the
valdoesn't have a backing field, likeval x get() = 1, then no field is generated.