Initialization of instance fields vs. local variables

前端 未结 7 571
余生分开走
余生分开走 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条回答
  •  旧时难觅i
    2020-11-27 06:39

    The implicit constructor initializes the instance variable for you. Even when you specify a c'tor but don't initialize a field, it's done for you as part of creating the object on the heap. That's not true of stack local variables.

提交回复
热议问题