javafinalhappens-before

Can we treat final reorder rule in Java as happens-before rule?


There exist two final record rules in Java, as follows:

  1. a write to final field in constructor and the constructed instance reference is assigned to variable afterwards can not be reordered
  2. read a instance reference and read the final field in the instance afterwards can not be reordered

Can we treat above rules as happen-before rules?

  1. a write to final field in constructor happens-before the constructed instance reference is assigned to variable afterwards
  2. read a instance reference happens-before read the final field in the instance afterwards

I think happens-before rule is stronger semantic.


Solution

  • In my understanding finally,the semantic guarantee of final keyword and happens-before rule belong to separate categories.The above derivation is wrong and unnecessary.