Initialization of instance fields vs. local variables

前端 未结 7 570
余生分开走
余生分开走 2020-11-27 06:07

I have always been wondering about why in the following example it is OK to not initialize the instance field (relying that it will have its default value)

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 06:25

    It's really just a matter of what the warning is able to tell you. There's really no way for the warning to be sure that some other method hasn't initialized the class variable, so it only warns on the one it can be certain isn't initialized.

    Also, it's a warning and not an error because there's nothing technically wrong with using the unassigned variable (it's guaranteed to be 'false') but it's probably a logic error to have it unassigned.

提交回复
热议问题