Nullable var with `?` vs. lateinit var

2020-02-15 02:37发布

What is the best way to define global variables in a Kotlin/Android activity/fragment?

What are the different scenarios when you should use these 2 methods for defining a global variable:

var viewpager: CustomViewPager? = null 

or

lateinit var viewpager: CustomViewPager

?

If I use the former, I won't have to check for null in my code. For example if I used lateinit for the following:

viewpager = activity?.findViewById<CustomViewPager>(R.id.viewpager) then I would have to check for null.

3条回答
你好瞎i
2楼-- · 2020-02-15 03:06

using lateinit, you are saying that you absolutely will make sure that an instance of that variable is created somewhere (otherwise, your application will throw an exception if a lateinit has not been initialized) and then that variable also will not be null throughout the rest of your project, compared to using null, it means that this object potentially could be null somewhere in your code for the rest of the project and you will have to deal with nullability throughout.

If you are positive that you are not going to make a variable null and you require an instance of it always, use lateinit

Ask yourself this question :

Am I 100% sure that I will be using an instance of this variable somewhere in this class ?

If the answer to that is Yes, you should probably be using lateinit, as lateinit forces you to create an instance of it.

If the answer is No, you should probably be using a nullable field instead.

Taken from here : https://www.kotlindevelopment.com/lateinit-kotlin/

The lateinit keyword stands for late initialization. Lateinit comes very handy when a non-null initializer cannot be supplied in the constructor, but the developer is certain that the variable will not be null when accessing it, thus avoiding null checks when referencing it later.

查看更多
迷人小祖宗
3楼-- · 2020-02-15 03:17

i would recommend using the first method, it's better cause it eliminates app crashes if the code is trying to access the viewPager while it was not initialized, you can also use this in order to access the viewPager in the first method to make sure your app won't crash

viewPager?.let{"it"

}

this will create an instance of the viewPager if it was already initialized and use it as a val instead of a var, the instance is called "it" and u can rename it as anything by doing this after the first {

viewPager?.let{viewPager ->

}
查看更多
够拽才男人
4楼-- · 2020-02-15 03:18

If you make sure that variable will never be null in any place of the code then use lateinit. Here you have to initialize it before using it (in your case you can initialize it inside onCreate(..). Make sure that the variable will never become null later ELSE a null pointer exception will be fired. In this way, you can directly use the variable without checking it if it's null or not. Also, you can detect if the variable has initialized or not using:

if (:: viewpager.isInitialized) { .... }

On the other hand, you should use the nullable option using ? In this case, you should check the variable before using it if it's null or not. you can use ?. for that. You also can combine that with let for example:

viewPager?.let {
 ....
}
查看更多
登录 后发表回答