loading...

How to use lazy in Kotlin

twitter logo github logo ・1 min read

We all have been there. Kotlin is good, and we want to declare a variable outside of onCreate() so that we can access the member variable anytime outside of it too. Now to make it non null, you have to either send some data and init the variable in the constructor. Which is not possible in most cases.

For performance reasons
Now don't just use lazy just like this:-

private val someString by lazy {
"someData"
}

but do like this :-

private val someString by lazy(LazyThreadSafetyMode.NONE) {
"someData"
}

https://www.youtube.com/watch?v=mNviUg0ocsk

twitter logo DISCUSS
Classic DEV Post from Jun 1 '19

What the heck is a Developer Advocate?

Pulkit profile image
Geek Android