Why can my instance initializer block reference a field before it is declared?

后端 未结 5 507
無奈伤痛
無奈伤痛 2021-01-18 03:19

My understanding is that you cannot reference a variable before it has been declared, and that all code (including instance initializers) that is within the body of a class,

5条回答
  •  孤街浪徒
    2021-01-18 04:04

    The contents of initializer block are executed whenever any constructor is invoked (before the constructor’s contents).

    So you can provide reference to any variables as they will not be used unless a constructor is invoked aka. object is created.

提交回复
热议问题