Top-level property initialization: when does it happen?

Kotlin doesn’t give you any guarantees when top level properties will be initialized. All you can say is that the initialization code will be called before you access a property, but you can’t necessarially tell when.

If you know which target platform you are on, you have a bit more information. I don’t know enough about native to help you there but on the JVM a top level property get’s initialized the when you call any top level function or property within the file. It’s a bit more complicated than that. It has to do with the JVM classloader and when that calls static initialization blocks (<clinit> function).
Maybe these links help to understand that:

I hope this helps with your first question (at least in regards to the JVM). The important part is that you can’t really put initialization code into top level properties and just expect them to be called in time. If you have some global initialization code that needs to be run for your library to work, it needs to be called explicitly by the consumer of your library.

2 Likes