title: Java Constructors and thread safety
body: <p>Literature talks about advertising a reference to an object before its constructor finished allocating and initializing its data structures. This usually involves putting it somewhere where other threads can see it prematurely. As I understand it, it involves an explicit act of advertising such as when using a Listener.</p>
  <p>My question relates to the implementation of a constructor and the possibility of something similar happening. I can imagine that a constructor can be implemented with something similar to:</p>
  <pre><code>Type t = new Type(...);
  </code></pre>
  <p>An implementation in C might do something like:</p>
  <pre><code>t = malloc(sizeOf Type);
  </code></pre>
  <p>And then proceed to initialize all the fields.</p>
  <p>If it can be implemented like this, then the reference t will be non-null, before the data is initialized. If another thread checks it for being non-null, it will then proceed to use it before it is fully initialized. The result will be mayhem.</p>
  <p>I cannot find anything that says that you cannot implement it like this. I am probably missing something pretty basic in my understanding of the Java Memory Model. Is there anything that instructs JVM implementors not to do it like this?</p>