I have a piece of code which looks like this:
Snippet A:
class Creature {
private static long numCreated;
public Creature() {
See http://download.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html#MemoryVisibility:
A write to a volatile field happens-before every subsequent read of that same field. Writes and reads of volatile fields have similar memory consistency effects as entering and exiting monitors, but do not entail mutual exclusion locking.
So the answer is yes. The write of the volatile in the constructor happens before the read of the volatile in numCreated()
. And since the non-atomic incrementation is still done in a synchronized block, the synchronization is alright (the incrementation is not atomic, but the write of the volatile long is).