Discussion
Before you can start using an object, Kotlin needs to run the code that will populate the object’s properties with their starting values.
But these two objects reference one another in an obvious cycle, making it impossible for that initialization to complete normally. You might expect the program to crash or enter an infinite loop, but instead, the attempt to read from an uninitialized property just returns null.
Surprisingly, that null value then ends up being stored as the value for both properties, in spite of their non-nullable type declarations.
Creating Objects
Object declarations such as the ones in this program are lazily initialized—they won’t run any setup code until they’re used for the first time. The first line of our ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access