title: java - volatile keyword also for non-primitives
body: <p>I am unsure if the volatile keyword should also be used for non-primitives. I have a class member which is set/assigned by one thread, and accessed by another thread. Should I declare this member volatile?</p>
  
  <pre><code>private /* volatile */ Object o;
  
  public void setMember(Object o) {
      this.o = o;
  }
  
  public Object getMember() {
      return o;
  }
  </code></pre>
  
  <p>Here, setMember(...) is called by one thread and getMember() is called by another one.</p>
  
  <p>If it was a boolean, for example, the answer would be yes.</p>
  
  <p>I am using Java 1.4 and the member in this case is read-only. So I am only caring about visibility in this case, hence my question about the volatile keyword.</p>