Java - final variables

前端 未结 7 1247
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 01:43

I know that once a final variable has a value assigned to it, it cannot be changed. However I just have a couple of questions regarding this:

  • When I have a

7条回答
  •  無奈伤痛
    2020-12-16 02:06

    final fields must be initialized indeed, as this will be their value for the rest of the program.

    An exception to that is that a final variable may be initialized in the constructor. Since static fields don't belong to an instance they must be initialized (there is no constructor) direct initialization, or static initialization blocks are options for doing that.

    Regarding your last question, yes, this is why it's called final.

提交回复
热议问题