When is it appropriate to use blank final variables?

前端 未结 9 1396
野趣味
野趣味 2020-12-16 00:24

I was looking at another question about final variables and noticed that you can declare final variables without initializing them (a blank final variable). Is there a reaso

9条回答
  •  死守一世寂寞
    2020-12-16 01:16

    From Wikipedia

    The blank final, which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer. A blank final can only be assigned once and must be unassigned when an assignment occurs. In order to do this, a Java compiler runs a flow analysis to ensure that, for every assignment to a blank final variable, the variable is definitely unassigned before the assignment; otherwise a compile-time error occurs.

    In general, a Java compiler will ensure that the blank final is not used until it is assigned a value and that once assigned a value, the now final variable cannot be reassigned another value.

提交回复
热议问题