Java: Final field freeze on object reachable from final fields

前端 未结 5 633
不知归路
不知归路 2021-01-02 03:25

DZone refcard titled \"Core Java Concurrency\" states:

Once set, final field values cannot be changed. Marking an object reference field as final do

5条回答
  •  余生分开走
    2021-01-02 03:57

    It does not really make sense to talk about what becomes final before what else. To your program, once your object is created (actually from the moment the field is assigned once) the reference can not change anymore. Since the B instance is created before the A instance, you could say c becomes final before b, but it does not really matter.

    Where the order is important is when you have multiple final fields in a single class. If you want to use the value of one final field in the assignment of another, you should only access fields that already have been initialized.

    To be honest, that 'final field freeze' sentence does not make much sense to me.

提交回复
热议问题