title: Synchronized objects accessed from unsynchronized methods
body: <p>Suppose I have a class in which there is a StringBuffer declared as a member variable. Two threads are trying to manipulate the object like below</p>
  
  <pre><code>public class SomeService {
  
      private StringBuffer sb = new StringBuffer();
  
      public void printName(String name) {
          sb.append(name);
          System.out.println(sb);
      }
  }
  
  public class StringBufferSynchronizationTest implements Runnable {
  
      private SomeService service = new SomeService();
  
      public StringBufferSynchronizationTest() {
  
          Thread thread = new Thread(this);
          thread.start();
      }
  
      public static void main(String[] args) {
  
          new StringBufferSynchronizationTest().service.printName("oops");
      }
  
      @Override
      public void run() {
          service.printName("java");
      }
  }
  </code></pre>
  
  <p>And I get this output</p>
  
  <pre><code>oopsjava
  oopsjava
  </code></pre>
  
  <p>I thought I will get</p>
  
  <pre><code>oops
  oopsjava
  </code></pre>
  
  <p>as output. When I synchronized printName method I got the second output. </p>
  
  <p>So I understood that even I use a synchronized class I have to synchronize blocks/methods that use the synchronized classes. Am I right?</p>