Val properties and bytecode

#1

Hi,
I have a question about the compiled bytecode. What happen with val properties?

class Foo (val arg1: Int, val arg2: String) { ... }

Do you make of them final attributes or private attributes with one getter method?

#2

Did you seen https://stackoverflow.com/questions/35538049/kotlin-bytecode-how-to-analyze-in-intellij-idea?

3 Likes
#3

Kotlin is uses final by default, so everything that isn’t marked as open uses final in the bytecode.

Properties are a bit more complicated. I think private properties are just compiled to a field. Anything else get’s compiled into a getter-function(val and var) as well as a setter-function for var. If the property requires it kotlin automatically adds a private backing field.

1 Like
#4

Thank you