Posted on 30th April 2025|42 views
What is the difference between kotlin lateinit and lazy?
Posted on 30th April 2025| views
Below you can see the difference between kotlin lateinit and lazy:
Usually, properties declared as holding a non-null type need to be initialized within the constructor. Still, frequently that is not suitable. For instance, features can be initialized by dependence injection, preferentially within the setup method like a unit test. While this case, you can't provide a non-null initializer within the constructor, however, you still need to avoid null checks while referencing that property within the class body.
lazy() is a function that exerts a lambda and returns an example of lazy which can assist as some substitute for performing a lazy property: the initial call to get() performs the lambda moved to lazy() and retains the result, following calls to get() naturally return the retained result.