[concurrency-interest] 3 rules of final field semantics

Mohan Radhakrishnan radhakrishnan.mohan at gmail.com
Mon Dec 26 05:21:30 EST 2011

After the recent discussion about final fields I decided to look up
the rules in the JMM.

It looks like rule 1 is in 17.5 Final Field Semantics with an example.

Do the programs shown below exemplfify rule 2 and 3 assuming one
thread calls writer and the other calls reader? The sentences
describing the rules are a little bit complex.

Similarly, you cannot reorder either of the first two with the third
assignment in:
      v.afield = 1; x.finalField = v; ... ; sharedRef = x;


public class FinalRule2 {

        private final V finalField;

        private V v;

        public static FinalRule2 rule2;

        public FinalRule2() {

        	v.afield = 1;

        	finalField = v;


        public static void writer(){
        	rule2 = new FinalRule2(); //sharedRef

        public static void reader(){
		if( null != rule2 ){
			System.out.println( rule2.v.afield );

class V{
	int afield;

The initial load (i.e., the very first encounter by a thread) of a
final field cannot be reordered with the initial load of the reference
to the object containing the final field. This comes into play in:
      x = sharedRef; ... ; i = x.finalField;


public class FinalRule3 {

        private final int finalField;

        private int i;

        public static FinalRule3 rule3;

        private FinalRule3 x;

        public FinalRule3(){
        	finalField = 1;

        public static void writer(){
        	rule3 = new FinalRule3();
        	x = rule3;                  //sharedRef

        public static void reader(){
			if( null != rule3 ){
				int i = x.finalField;
				System.out.println( i );


More information about the Concurrency-interest mailing list