# Initialize global variable via function return value

**URL:** <https://discuss.kotlinlang.org/t/initialize-global-variable-via-function-return-value/17760>\
**Category:** Support\
**Created:** [May 19, 2020, 4:02pm UTC](https://discuss.kotlinlang.org/t/initialize-global-variable-via-function-return-value/17760 "2020-05-19T16:02:15Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![benwoodworth](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@benwoodworth](https://discuss.kotlinlang.org/u/benwoodworth)\
**Post date:** [May 20, 2020, 1:31am UTC](https://discuss.kotlinlang.org/t/initialize-global-variable-via-function-return-value/17760/7 "2020-05-20T01:31:09Z")

</div>

In the JVM, static fields in a class are initialized when the class is first used. And Kotlin properties at the top level are stored in static fields.

So, once anything in that file is used (like a function, or something else), then the field will be initialized. If there’s a function that must be called in order to use your library, then putting it in the same file as that property will guarantee it’s initialized (and it’ll be initialized before the function’s called)

---

_[View the full topic](https://discuss.kotlinlang.org/t/initialize-global-variable-via-function-return-value/17760)._
