4 ms·
See section 17.5, "final Field Semantics" in http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf. Note t
by andars 9y ago
See section 17.5, "final Field Semantics" in
http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf http://docs.oracle.com/javase/specs/jls/se8/jls8.pdf.
Note that a String's value field is final.
- alkonaut 9y agoExactly. Relevant passage from ^ An object is considered to be completely initialized when its constructor finishes. A thread that can only see a reference to an object after that object has been completely initialized is guaranteed to see the correctly initialized values for that object's final fields.
- readittwice 9y agoThanks, that explains it! Didn't know that final fields have this special rule and that the value field in the String class is final. So to recap: this shouldn't be possible for Strings, since the value field in the String class is final and therefore has a memory barrier. But it could happen for other classes with non-final fields.