# KMM Oddity #2: Initialization order of top-level properties

DevFeed: [KMM Oddity #2: Initialization order of top-level properties](<https://devfeed.tech/articles/kmm-oddity-2-initialization-order-of-top-level-properties-24738.md>)

Original publisher: [Read original article](<https://medium.com/xorum-io/kmm-oddity-2-initialization-order-of-top-level-properties-edd11ec350a2?source=rss----92bb7980cc9f---4>)

Author: Yev Kanivets

Published: 2021-11-15T15:44:44Z

Content type: tutorial

Language: en

Sources: [xorum.io - Medium](<https://devfeed.tech/sources/xorum-io-medium.md>)

Topics: [Kotlin Multiplatform](<https://devfeed.tech/topics/kotlin-multiplatform.md>), [kotlin-native](<https://devfeed.tech/topics/kotlin-native.md>), [Android](<https://devfeed.tech/topics/android.md>), [iOS](<https://devfeed.tech/topics/ios.md>), [Redux](<https://devfeed.tech/topics/redux.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [ios](<https://devfeed.tech/tags/ios.md>), [kmm](<https://devfeed.tech/tags/kmm.md>), [kmp](<https://devfeed.tech/tags/kmp.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [kotlin-native](<https://devfeed.tech/tags/kotlin-native.md>), [mobile](<https://devfeed.tech/tags/mobile.md>), [redux](<https://devfeed.tech/tags/redux.md>)

## AI overview

This article examines an initialization-order oddity involving Kotlin top-level properties in Kotlin Multiplatform Mobile. It compares behavior when dependent properties are declared in one file or separate files, including differing initialization sequences in Android and iOS applications.

## Source excerpt

Kotlin Multiplatform goes Beta this Spring (2022), so it removes all major hassles (Memory Model and Kotlin/Native concurrency, for example) which KMP developers face. But even with the Stable version (hopefully by the end of 2022), we are going to have a certain number of minor issues or simply oddities while using this relatively young technology (especially with Kotlin/Native). Most of them are already reported in Kotlin's YouTrack, so go check it before stressing out. I've encountered many such issues myself for the past two years with Kotlin Multiplatform Mobile. In this series of articles, I'll share my favorite oddities of KMM with explanations and workarounds (if possible). Top-level properties In Kotlin, we are able to declare top-level (or global) variables, which are accessible from anywhere in the given module or even the whole application. For example, let's say that we have a data class Order: https://medium.com/media/a25461d8a0d6b9b56cb9dd912331f029/href This class has a name and an optional dependency to another Order. The init block is here to help us understand when it's initialized. While it's generally a bad idea to declare instances of such class as a global variable, we are going to do it twice in the same file for now. As you can see, orderB depends on orderA, so it must be initialized first. https://medium.com/media/b290af047e51b599b293e0aca4e01fee/href It's done for demo purposes here, but a real application can require it as well (think about store, reducers, and middleware from Redux architectural pattern, which are all global variables by default). When run, we see the expected order of initialization in logs of both, Android and iOS applications: Init orderA / Init orderB. Single-file initialization order When we have all connected top-level variables in the same file, things are simple. To check it, swap the orderA with orderB. https://medium.com/media/25fc0522be792d1cfa0da6cbd4ec7f90/href Do you see? Android Studio will complain about