title: Synchronization of non-final field
body: <p>A warning is showing every time I synchronize on a non-final class field. Here is the code:  </p>
  
  <pre><code>public class X  
  {  
     private Object o;  
  
     public void setO(Object o)  
     {  
       this.o = o;  
     }  
  
     public void x()  
     {  
       synchronized (o) // synchronization on a non-final field  
       {  
       }  
     }  
   } 
  </code></pre>
  
  <p>so I changed the coding in the following way:</p>
  
  <pre><code> public class X  
   {  
  
     private final Object o;       
     public X()
     {  
       o = new Object();  
     }  
  
     public void x()  
     {  
       synchronized (o)
       {  
       }  
     }  
   }  
  </code></pre>
  
  <p>I am not sure the above code is the proper way to synchronize on a non-final class field. How can I synchronize a non final field? </p>